簡體   English   中英

Python 3.1.2 + Snow Leopard + lxml + XMLSchema

[英]Python 3.1.2 + Snow Leopard + lxml + XMLSchema

我想使用lxml庫來驗證Python 3.1.2中的XML模式。

由於Snow Leopard MAC OS隨附安裝了Python 2.6.1,因此,我首先從http://www.python.org/ftp/python/3.1.2/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分發站點軟件包目錄(/Library/Python/2.6/site-packages)上安裝了該軟件包,但沒有問題,但我希望將其安裝在我的Python 3.1分發站點軟件包目錄(/ Library /Frameworks/Python.framework/Versions/3.1/lib/python3.1/site-packages)。

我試圖用python3 setup.py install替換python setup.py install ,但是控制台中有很多錯誤消息。 使用easy_install lxml安裝easy_install lxml具有相同的效果。

作為最后的選擇,我只是嘗試將Python 2.6分發site-packages目錄的內容移動到Python 3.1分發site-packages目錄,並運行如下測試腳本:

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+

根據Ned 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

如何使lxml在Python 3.1中運行?

提前致謝。

如果要從源代碼構建lxml,則需要使用所需的Python完全構建它。 而且,通常,您不能僅將站點包從一個Python實例移動到另一個,尤其是Python 2 vs Python3。首先,撤消所做的任何復制或移動到Python 3 site-packages目錄中。 如果不確定自己做了什么,則應考慮重新安裝Python 3.1。 現在從tar文件的lxml源的干凈副本開始,並嘗試使用python3.1進行構建:

$ rm -r lxml-2.2.6
$ 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

如果遇到錯誤,請更新您的問題以確切顯示出現什么錯誤消息。

編輯:相關的錯誤消息是此:

Compiling with an SDK that doesn't seem to exist: /Developer/SDKs/MacOSX10.4u.sdk

大多數python.org python都可以在OS X的多個版本上運行,因此它們使用可選的10.4 SDK。 Apple的10.6 Snow Leopard的Xcode安裝程序包括10.4u SDK,但默認情況下未安裝。 您需要使用Xcode安裝程序(在Snow Leopard DVD上或新Mac附帶或從Apple Developer Connection下載)中安裝它。

我也有在Mac上安裝lxml的問題,但這對我有用:

sudo env ARCHFLAGS="-arch i386 -arch x86_64" easy_install lxml

暫無
暫無

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

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