将lxml.etree导入python时出错

时间:2012-11-13 05:28:01

标签: python python-2.7 lxml

我在我的mac上安装了一个lxml,当我输入像这样的python

localhost:lxml-3.0.1 apple$ python
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from lxml import etree
Traceback (most recent call last):
  File "", line 1, in 
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lxml-3.0.1-py2.7-macosx-10.6-intel.egg/lxml/etree.so, 2): Symbol not found: ___xmlStructuredErrorContext
  Referenced from: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lxml-3.0.1-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-3.0.1-py2.7-macosx-10.6-intel.egg/lxml/etree.so

3 个答案:

答案 0 :(得分:7)

我遇到了同样的问题。如果您使用pip安装它,如下所示:pip install lxml

相反,请尝试使用

STATIC_DEPS=true pip install lxml

这解决了我的问题。

找到this website

答案 1 :(得分:5)

如果您已经安装了libxml2,那么它可能就是没有选择正确的版本(默认情况下安装了OS X版本)。特别是,假设您已将libxml2安装到/usr/local。您可以查看共享库etree.so引用的内容:

$> otool -L /Library/Python/2.7/site-packages/lxml-3.2.1-py2.7-macosx-10.7-intel.egg/lxml/etree.so 
/Library/Python/2.7/site-packages/lxml-3.2.1-py2.7-macosx-10.7-intel.egg/lxml/etree.so:
    /usr/lib/libxslt.1.dylib (compatibility version 3.0.0, current version 3.24.0)
    /usr/local/lib/libexslt.0.dylib (compatibility version 9.0.0, current version 9.17.0)
    /usr/lib/libxml2.2.dylib (compatibility version 10.0.0, current version 10.3.0)
    /usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.5)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)

在系统安装版本中检查该符号:

$> nm /usr/lib/libxml2.2.dylib | grep ___xmlStructuredErrorContext

对我来说,它不存在于系统安装的库中。但是在我安装的版本中:

$> nm /usr/local/lib/libxml2.2.dylib | grep ___xmlStructuredErrorContext
000000000007dec0 T ___xmlStructuredErrorContext

要解决此问题,请确保您的安装路径首先显示在DYLD_LIBRARY_PATH

$> export DYLD_LIBRARY_PATH=/usr/local/lib
$> python
>>> from lxml import etree
# Success!

答案 2 :(得分:-1)

运行以下命令安装lxml软件包。

pip install lxml --user 

应该解决问题。我在MAC OSX上测试过它 10.7.5,它工作正常。