首先,我在没有使用 pip(Mac OS 10.6.8 上的 Python 2.7.2)的情况下安装了 lxml。然后,我读了 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 版本安装单独的 easy_install
。查看答案here更多细节。然后您可以使用它运行 easy_install
命令:
STATIC_DEPS=true easy_install-2.7 lxml
更新:根据您的评论,您现在正在报告显示另一个 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
关于python - 在 mac 上使用 python 安装 lxml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9856767/