我想在Python 3.1.2中使用lxml庫來驗證XML Schema。Python 3.1.2 + Snow Leopard + lxml + XMLSchema
由於Snow Leopard MAC OS安裝了Python 2.6.1,我首先下載了Python 3.1.2自動安裝程序http://www.python.org/ftp/python/3.1.2/python-3.1.2-macosx10.3-2010-03-24.dmg並安裝它。
其次,我在http://pypi.python.org/packages/source/l/lxml/lxml-2.2.6.tar.gz下載LXML 2.2.6,解壓,並在規定進行安裝http://wiki.python.org/moin/CheeseShopTutorial(即:)
$ cd lxml-2.2.6
$ python setup.py install
它與我的Python 2.6分發定點後患安裝包packages目錄(/Library/Python/2.6/site-packages),但是我想將它安裝在我的Python 3.1發行版的站點包目錄中(/Library/Frameworks/Python.framework/Versions/3.1/lib/python3 ·1 /站點包)。
我試圖用python3 setup.py install
替換python setup.py install
,但是我在控制檯中有很多錯誤消息。使用easy_install lxml
安裝lxml具有相同的效果。
作爲最後的手段,我想簡單地添加到Python 2.6分發site-packages目錄的內容移動到了Python 3.1分發站點包目錄並運行測試腳本是這樣的:
try:
from lxml import etree
print("running with lxml.etree")
except ImportError:
try:
# Python 2.5
import xml.etree.cElementTree as etree
print("running with cElementTree on Python 2.5+")
except ImportError:
try:
# Python 2.5
import xml.etree.ElementTree as etree
print("running with ElementTree on Python 2.5+")
except ImportError:
try:
# normal cElementTree install
import cElementTree as etree
print("running with cElementTree")
except ImportError:
try:
# normal ElementTree install
import elementtree.ElementTree as etree
print("running with ElementTree")
except ImportError:
print("Failed to import ElementTree from any known place")
schema_root = etree.parse('note.xsd').getroot()
schema = etree.XMLSchema(schema_root)
parser = etree.XMLParser(schema = schema)
root = etree.parse('note.xml', parser)
而且我得到了在控制檯此錯誤消息:
Traceback (most recent call last):
File "/Users/eduardo/Workspace/PythonToolbox/TestProject/src/testproject/domparse.py", line 97, in <module>
schema = etree.XMLSchema(schema_root)
AttributeError: 'module' object has no attribute 'XMLSchema'
running with cElementTree on Python 2.5+
正如斯內德Deily的建議,我做了以下內容:
$ curl http://pypi.python.org/packages/source/l/lxml/lxml-2.2.6.tar.gz | tar xz
$ cd lxml-2.2.6
$ python3 setup.py install
但我有一些編譯器錯誤消息,文件http://www.educoelho.com/files/output.txt
我怎樣才能獲得在Python 3.1中運行lxml?
在此先感謝。
嘿斯內德,感謝您的答覆。我將編輯顯示錯誤消息的問題。 – 2010-07-07 19:18:52
啊,看看上面編輯的答案。 – 2010-07-07 19:51:18
謝謝Ned Deily!我下載了最新版本的SDK(xcode_3.2.3_and_iphone_sdk_4__final.dmg),並重新安裝了標有MacOSX10.4u.sdk複選框的XCode/SDK(這不是默認設置)。現在我的/ Developer/SDKs目錄也有MacOSX10.4u.sdk。我重複了你的安裝步驟,現在lxml在Python 3.1.2中正常工作。謝謝! – 2010-07-07 22:34:53