简体   繁体   English

在mac上用python安装lxml

[英]Installing lxml with python on a mac

First, I installed lxml without using pip (Python 2.7.2 on Mac OS 10.6.8). 首先,我在不使用pip的情况下安装了lxml(Mac OS 10.6.8上的Python 2.7.2)。 Then, I read this post and I installed it again using pip ( sudo pip install lxml ). 然后,我读了这篇文章,然后我再次使用pip( sudo pip install lxml )安装它。 I still had a problem: 我还有一个问题:

I can import lxml ( import lxml ) but I cannot use from lxml import etree . 我可以导入lxml( import lxml )但我无法使用from lxml import etree I have this error message: 我有这个错误消息:

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

Then, I tried to install lxml from source following these instructions , and I have this error message: 然后,我尝试按照这些说明从源安装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

Finally, I followed the second advise of this answer and I used the command line sudo STATIC_DEPS=true /usr/bin/easy_install-2.7 lxml . 最后,我按照这个答案的第二个建议,我使用命令行sudo STATIC_DEPS=true /usr/bin/easy_install-2.7 lxml It installed lxml on the Apple-supplied system Python 2.7, and not on the version I'm currently using. 它在Apple提供的系统Python 2.7上安装了lxml,而不是我当前使用的版本。 The positive point: if I run the Apple-Python, I can from lxml import etree . 积极的一点:如果我运行Apple-Python,我可以from lxml import etree

Negative point: I still don't know how to install lxml on another version of python. 否定点:我仍然不知道如何在另一个版本的python上安装lxml。 Any idea? 任何的想法?

I'm currently using /Library/Frameworks/Python.framework/Versions/2.7/bin/python . 我目前正在使用/Library/Frameworks/Python.framework/Versions/2.7/bin/python

You need to install a separate easy_install for the version of Python you are using. 您需要为正在使用的Python版本安装单独的easy_install See the answer here for more details. 有关详细信息,请参阅此处的答案。 Then you can run the easy_install command using it: 然后,您可以使用它运行easy_install命令:

STATIC_DEPS=true easy_install-2.7 lxml

UPDATE: From your comments, you now are reporting a permission error showing yet another Python path, one that appears to be a MacPorts-installed Python: /opt/local/Library/Frameworks . 更新:从您的评论中,您现在报告的权限错误显示另一个Python路径,一个似乎是MacPorts安装的Python: /opt/local/Library/Frameworks You need to figure out which Python you want to use. 您需要确定要使用的Python。 If, in fact, you want to use the MacPorts one, then simply install the MacPorts-provided lxml port. 事实上,如果你想使用MacPorts,那么只需安装MacPorts提供的lxml端口即可。 That's the easiest solution. 这是最简单的解决方案。

$ sudo port selfupdate
$ sudo port install py27-lxml

Otherwise, you need to install easy_install with the other (python.org?) Python 2.7.2. 否则,您需要使用另一个(python.org?)Python 2.7.2安装easy_install I would recommend to use Distribute , the more modern fork of `setuptools' and you need to ensure you are using the right Python: 我建议使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM