2012-11-13 61 views
7

我已經安裝在我的Mac的LXML,當我在Python這樣出現錯誤時,進口lxml.etree到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 
+2

本文可能幫助:[在Mac OS X 10.7上的Virtualenv中構建Python Lxml](http://roderickhodgson.com/blog/2012/10/27/building-python-lxml-on-mac-os- x-10-dot-7 /)(你可能會鏈接到_libxml2_的舊版本,而不是_lxml_所需的版本)。 –

+0

我會考慮Virtualenv,但我可以在當前的Python安裝lxml基礎? – heghogbbb

+0

謝謝,我將在對此解決方案進行測試後返回此處 – heghogbbb

回答

7

我有同樣的問題類型。如果你有點子安裝它,如下所示:pip install lxml

相反,嘗試使用

STATIC_DEPS=true pip install lxml 

這解決了這個問題對我來說。

this website

+0

謝謝,我已經在幾十次測試後安裝了這個軟件包。但你的解決方案似乎很好。 – heghogbbb

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! 
+0

經過一個小時的嘗試,這解決了我的問題......這個問題的永久解決方案是什麼? – eaponte

+0

您可以將此路徑添加到'/etc/ld.so.conf.d/lxml.conf'(創建一個新文件),然後運行'ldconfig'(http://man7.org/linux/man-pages /man8/ldconfig.8.html) –

+0

對我來說,做相反的事情解決了我的問題(因爲符號實際上是在系統的libxml2版本中),所以我必須將/ usr/lib作爲DYLD_LIBRARY_PATH的第一個條目 – knocte

-1

運行以下命令來安裝lxml的包。

pip install lxml --user 

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