简体   繁体   English

将 lxml.etree 导入 python 时出错

[英]get errors when import lxml.etree to python

i have installed an lxml on my mac, when i type in python like this我在我的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

I had the same problem. 我有同样的问题。 If you have installed it with pip as follows: pip install lxml 如果您使用pip安装它,如下所示: pip install lxml

Instead, try to use 相反,尝试使用

STATIC_DEPS=true pip install lxml

This solved the problem for me. 这解决了我的问题。

Found at this website 这个网站上找到

If you've installed libxml2 , then it's possible that it's just not picking up the right version (there's a version installed with OS X by default). 如果您已经安装了libxml2 ,那么它可能就是没有选择正确的版本(默认情况下安装了OS X版本)。 In particular, suppose you've installed libxml2 to /usr/local . 特别是,假设您已将libxml2安装到/usr/local You can check what shared libraries etree.so references: 您可以检查哪些共享库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)

Checking for that symbol in the system-installed version: 在系统安装的版本中检查该符号:

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

For me, it's not present in the system-installed library. 对我来说,它不存在于系统安装的库中。 In the version I installed, however: 但是,在我安装的版本中:

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

To solve this, make sure your install path appears first in DYLD_LIBRARY_PATH : 要解决此问题,请确保您的安装路径首先出现在DYLD_LIBRARY_PATH

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

If you are having this problem in 2022 on an M1 Mac, try this:如果您在 2022 年在 M1 Mac 上遇到此问题,请尝试以下操作:

pip3 uninstall lxml

pip3 install lxml

This will both upgrade lxml to the latest version (4.7.1 vs. 4.6.3 in my case) and clear files that could be causing issues per other anecdotal evidence on this problem.这既会将 lxml 升级到最新版本(在我的情况下为 4.7.1 与 4.6.3),并根据有关此问题的其他轶事证据清除可能导致问题的文件。

Run the following command to install the lxml package. 运行以下命令以安装lxml软件包。

pip install lxml --user 

should fix the issue. 应该解决这个问题。 I tested it on MAC OSX 10.7.5, it worked fine. 我在MAC OSX 10.7.5上测试过,它工作正常。

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

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