簡體   English   中英

將 lxml.etree 導入 python 時出錯

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

我在我的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

我有同樣的問題。 如果您使用pip安裝它,如下所示: pip install lxml

相反,嘗試使用

STATIC_DEPS=true pip install lxml

這解決了我的問題。

這個網站上找到

如果您已經安裝了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!

如果您在 2022 年在 M1 Mac 上遇到此問題,請嘗試以下操作:

pip3 uninstall lxml

pip3 install lxml

這既會將 lxml 升級到最新版本(在我的情況下為 4.7.1 與 4.6.3),並根據有關此問題的其他軼事證據清除可能導致問題的文件。

運行以下命令以安裝lxml軟件包。

pip install lxml --user 

應該解決這個問題。 我在MAC OSX 10.7.5上測試過,它工作正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM