Python 3.1.2 +雪豹 + lxml + XML模式

发布于 2024-09-08 15:51:12 字数 3039 浏览 3 评论 0 原文

我想使用 lxml 库来验证 Python 3.1.2 中的 XML 模式。

由于Snow Leopard MAC操作系统预装了Python 2.6.1,首先,我在http://www.python.org/ftp/python/3.1.2/python-3.1.2-macosx10 .3-2010-03-24.dmg 并安装它。

其次,我在 http 下载了 lxml 2.2.6 ://pypi.python.org/packages/source/l/lxml/lxml-2.2.6.tar.gz,将其解压并按照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 发行版 site-packages 目录中 (/Library/Frameworks/Python.framework/Versions/3.1/lib/ python3.1/site-packages)。

我尝试将 python setup.py install 替换为 python3 setup.py install,但控制台中出现很多错误消息。使用 easy_install lxml 安装 lxml 具有相同的效果。

最后的手段,我尝试简单地将 Python 2.6 分发站点包目录的内容移动到 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+

作为 作者: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 中运行?

提前致谢。

I'd like to use lxml library to validate XML Schemas in Python 3.1.2.

Since the Snow Leopard MAC OS comes with the Python 2.6.1 installed, firstly, I downloaded the Python 3.1.2 automated installer at http://www.python.org/ftp/python/3.1.2/python-3.1.2-macosx10.3-2010-03-24.dmg and installed it.

Secondly, I downloaded lxml 2.2.6 at http://pypi.python.org/packages/source/l/lxml/lxml-2.2.6.tar.gz, unpacked it and performed the installation as stated in http://wiki.python.org/moin/CheeseShopTutorial (i.e.:)

  $ cd lxml-2.2.6
  $ python setup.py install

It installed the package with no troubles at my Python 2.6 distribution site-packages directory (/Library/Python/2.6/site-packages), but I'd like to have it installed in my Python 3.1 distribution site-packages directory (/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/site-packages).

I tried to replace python setup.py install with python3 setup.py install, but I've got a lot of error messages in the console. Installing the lxml by using easy_install lxml had the same effect.

As a last resort, I tried simply to move the content of the Python 2.6 distribution site-packages directory to the Python 3.1 distribution site-packages directory and run a test script like this:

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)

And I got this error message in the console:

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+

As suggested by Ned Deily, I did the following:

$ 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

But I've got some compiler error messages, the file http://www.educoelho.com/files/output.txt

How can I get lxml running in Python 3.1?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一指流沙 2024-09-15 15:51:12

如果要从源代码构建 lxml,则需要完全使用所需的 Python 来构建它。而且,一般来说,您不能只是将站点包从一个 Python 实例移动到另一个 Python 实例,尤其是 Python 2 与 Python 3。首先,撤消您对 Python 3 站点包目录所做的任何复制或移动。如果您不确定自己做了什么,您应该考虑重新安装 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 下载)来安装它。

If you are going to build lxml from source, you need to build it entirely with the desired Python. And, in general, you cannot just move site-packages from one Python instance to another, especially Python 2 vs Python 3. First, undo whatever copy or move you did into the Python 3 site-packages directory. If you are not sure what you did, you should consider re-installing Python 3.1. Now start with a clean copy of the lxml source from the tar file and try building it with 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

If you get errors, update your question to show exactly what error messages appear.

EDIT: The relevant error message is this one:

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

Most python.org pythons are built to work on several versions of OS X so they use the optional 10.4 SDK. Apple's Xcode installer for 10.6 Snow Leopard includes the 10.4u SDK but it is not installed by default. You need to install it using the Xcode installer (on the Snow Leopard DVD or that came with a new Mac or download from the Apple Developer Connection).

半夏半凉 2024-09-15 15:51:12

我在 Mac 上安装 lxml 时也遇到了问题,但这对我有用:

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

I also had problems to install lxml on Mac, but this worked for me:

sudo env ARCHFLAGS="-arch i386 -arch x86_64" easy_install lxml
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文