【问题标题】:Installing lxml with python on a mac在 Mac 上用 python 安装 lxml
【发布时间】:2012-03-24 23:58:02
【问题描述】:

首先,我在不使用 pip 的情况下安装了 lxml(Mac OS 10.6.8 上的 Python 2.7.2)。然后,我阅读了this post 并使用 pip (sudo pip install lxml) 再次安装了它。我还是有问题:

我可以导入 lxml (import lxml) 但我不能使用 from lxml import etree。我有这个错误信息:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lxml-2.3.3-py2.7-macosx-10.6-intel.egg/lxml/etree.so, 2): Symbol not found: _htmlParseChunk
  Referenced from: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lxml-2.3.3-py2.7-macosx-10.6-intel.egg/lxml/etree.so
  Expected in: flat namespace
 in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lxml-2.3.3-py2.7-macosx-10.6-intel.egg/lxml/etree.so

然后,我尝试从these instructions 之后的源代码安装 lxml,我收到以下错误消息:

checking whether we are cross compiling... configure: error: in `/Users/my_name/Applications/lxml/lxml-2.2.2/build/tmp/libxml2-2.7.8':
configure: error: cannot run C compiled programs.
If you meant to cross compile, use `--host'.
See `config.log' for more details
Traceback (most recent call last):
  File "setup.py", line 115, in <module>
    STATIC_CFLAGS, STATIC_BINARIES),
  File "/Users/my_name/Applications/lxml/lxml-2.2.2/setupinfo.py", line 50, in ext_modules
    libxslt_version=OPTION_LIBXSLT_VERSION)
  File "/Users/my_name/Applications/lxml/lxml-2.2.2/buildlibxml.py", line 198, in build_libxml2xslt
    call_subprocess(configure_cmd, cwd=libxml2_dir, **call_setup)
  File "/Users/my_name/Applications/lxml/lxml-2.2.2/buildlibxml.py", line 158, in call_subprocess
    raise Exception('Command "%s" returned code %s' % (cmd_desc, returncode))
Exception: Command "./configure --without-python --disable-dependency-tracking --disable-shared --prefix=/Users/my_name/Applications/lxml/lxml-2.2.2/build/tmp/libxml2" returned code 1

最后,我按照this answer 的第二个建议,使用了命令行sudo STATIC_DEPS=true /usr/bin/easy_install-2.7 lxml。它在 Apple 提供的系统 Python 2.7 上安装了 lxml,而不是在我当前使用的版本上。积极的一点:如果我运行 Apple-Python,我可以from lxml import etree

负面点:我仍然不知道如何在另一个版本的python上安装lxml。有什么想法吗?

我目前正在使用/Library/Frameworks/Python.framework/Versions/2.7/bin/python

【问题讨论】:

标签: python installation lxml importerror elementtree


【解决方案1】:

您需要为您使用的 Python 版本安装单独的easy_install。有关详细信息,请参阅答案here。然后你可以使用它运行easy_install 命令:

STATIC_DEPS=true easy_install-2.7 lxml

更新:从您的 cmets 中,您现在报告显示另一个 Python 路径的权限错误,该路径似乎是 MacPorts 安装的 Python:/opt/local/Library/Frameworks。您需要弄清楚要使用哪个 Python。如果,事实上,你想使用 MacPorts 之一,那么只需安装 MacPorts 提供的lxml 端口。这是最简单的解决方案。

$ sudo port selfupdate
$ sudo port install py27-lxml

否则,您需要将easy_install 与另一个(python.org?)Python 2.7.2 一起安装。我建议使用Distribute,它是“setuptools”的更现代的分支,您需要确保使用正确的 Python:

$ curl -O http://python-distribute.org/distribute_setup.py
$ export PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
$ which python2.7   # should be in the path above
$ python2.7 distribute_setup.py
$ STATIC_DEPS=true easy_install-2.7 lxml

【讨论】:

  • 所以首先我使用sh setuptools-0.6c11-py2.7.egg安装了easy_install(在下载了鸡蛋here之后,然后我修改了我的路径(export PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"),最后我安装了lxml(STATIC_DEPS=true easy_install-2.7 lxml)。仍然有最后一条命令有问题:error: can't create or remove files in install directory
  • The following error occurred while trying to add or remove files in the installation directory: [Errno 13] Permission denied: '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/test-easy-install-1180.write-test'}
  • 对了,和STATIC_DEPS=true easy_install lxml有什么区别? (没有-2.7)
  • 查看更新后的答案。分发和 setuptools 安装命令的未版本化 (easy_install) 和版本化 (easy_install-2.7) 副本。在特定的 Python 实例中,它们应该产生相同的结果。
  • @Antonin:添加的-2.7 指的是easy_install 的特定版本,因为您安装了多个python。如果您使用的是 macports,我实际上想把它扔掉......我建议完全删除它并使用 Homebrew。
猜你喜欢
  • 2015-08-05
  • 1970-01-01
  • 1970-01-01
  • 2013-11-02
  • 2022-07-02
  • 2016-10-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多