编程语言
首页 > 编程语言> > 在Mac上使用python安装lxml

在Mac上使用python安装lxml

作者:互联网

首先,我在不使用pip的情况下安装了lxml(在Mac OS 10.6.8上为Python 2.7.2).然后,我读了this post,然后使用pip(sudo pip install lxml)再次安装了它.我仍然有一个问题:

我可以导入lxml(导入lxml),但是不能从lxml导入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,而不是在我当前使用的版本上安装了lxml.积极的一点:如果运行Apple-Python,我可以从lxml导入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

否则,您需要与其他(python.org?)Python 2.7.2一起安装easy_install.我建议使用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

标签:importerror,lxml,installation,elementtree,python
来源: https://codeday.me/bug/20191101/1984323.html