无法在MAC上导入python模块lxml

时间:2017-11-05 04:34:05

标签: macos python-2.7 pip lxml

无法从MAC上的终端导入lxml。得到以下错误

错误:

python
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 12:01:12) 
[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 "<stdin>", line 1, in <module>
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lxml/etree.so, 2): Symbol not found: _PyUnicodeUCS4_AsASCIIString
  Referenced from: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lxml/etree.so
  Expected in: dynamic lookup

>>> 

'import lxml' works fine

Python路径

which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python

当我寻找lxml安装时,一切看起来都很好

pip install lxml
Requirement already satisfied: lxml in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

pip2 install lxml
Requirement already satisfied: lxml in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

我在这里缺少什么?什么可能是错的?能帮帮我吗。

解: 通过卸载lxml并按pip install lxml --no-binary :all:

重新安装来解决此问题

1 个答案:

答案 0 :(得分:0)

您使用UCS4(4字节unicode)编译python,使用UCS2编译lxml。即,扩展和python是不兼容的。更改其中一个 - 即,使用--enable-unicode=ucs2重新编译python或使用UCS4重新编译lxml。