2010-02-01 42 views
2

我想构建一个应用程序,使用一些xml数据使用Python的内置xml.etree.ElementTree类。当我从命令行运行时它工作正常,但是当我构建它时,出现“ImportError:No module etree.ElementTree”错误。我猜这是因为我没有正确导入该模块,但我一直无法弄清楚。当我使用“includes”或“packages”指令时,py2app会抱怨同样的错误,并且当我特别指定package_dir(/ System/Library/...)时,它会编译,但仍然会给我错误。我列举了一个简短的例子来说明这个问题。py2app和xml.etree.ElementTree

macxml.py

from xml.etree.ElementTree import ElementTree 

if __name__ == '__main__': 
    tree = ElementTree() 
    print tree.parse('lib.xml') 

这应该打印出 “在XXXXXX <元素库>”,其中图书馆是根名称。

setup.py

from setuptools import setup 

setup(name="Mac XML Test", 
     app=['macxml.py'], 
    ) 

是什么力量让Mac应用程序使用这个库的正确方法是什么?

的Python 2.6.4

的Mac OS X 10.6.2

编辑:我也使用Python 2.6.2尝试这种在另一台Mac(PPC 10.5.8),并取得相同的结果。

回答

1

重新安装和更新macholib,modulegraph,py2app和setuptools的无果后,我做了一点挖掘,发现modulegraph模块以下错误:

graphmodule.find_modules.find_modules(includes=['xml.etree']) 
Traceback (most recent call last): 
    File "<stdin>", line 1 in <module> 
    File ".../modulegraph/find_modules.py", line 255 in find_modules 
    File ".../modulegraph/find_modules.py", line 182 in find_needed_modules 
    File ".../modulegraph/modulegraph.py", line 401 in import_hook 
    File ".../modulegraph/modulegraph.py", line 464 in load_tail 
ImportError: No module named xml.etree 

所以我看起来更进load_tailimport_hook函数,发现由于某种原因,它正确地导入了xml包,但后来又去了一个旧的_xmlplus安装来查找etree子包(当然找不到它)。卸下_xmlplus包消除了错误,我能得到应用具有以下setup.py文件的工作:

from setuptools import setup 
import xml.etree.ElementTree 

setup(name="Mac XML Test", 
     app=['macxml.py'], 
     options={'py2app': {'includes': ['xml.etree.ElementTree']}}, 
     data_files=[('', ['lib.xml'])] 
    ) 

输出在控制台中显示出来。

+0

假设我们做了同样的事情,这对我也有效,因为下面的格式不好,我把它添加到下面的评论。编辑find_modules.py和改变 REPLACEPACKAGES = { '_xmlplus': 'XML', } 到 REPLACEPACKAGES = {# '_ xmlplus': 'XML', } – Steven 2011-01-07 16:54:24

0

如果您使用的是macports(或fink等),请确保py2app正在使用正确的解释器。您可能需要安装新版本才能使用2.6.x(在OS X 10.5上,py2app使用2.4.x)。

如果不工作,我的步骤,通过路径问题的工作是:

  1. 启动Python解释器,你的代码(或py2app)使用(你绝对肯定的??尝试which python
  2. import sys; print sys.path

如果步骤2.向你的代码是在错误的解释运行在/System/Library..someotherpythonversion的路径。

+0

我使用python.org的标准2.6.4安装。代码似乎是从正确的解释器运行的。 '哪个python'和sys.path都指向'/ Library/Frameworks/Python.framework/Versions/2.6 /'。 – jeffaudio 2010-02-01 20:24:05

+0

用'py2app'必须是一些奇怪的行为,那么你是如何安装它的?你有没有试过通过'py2app'运行脚本来打印路径? – 2010-02-02 03:08:55

+0

你说“当我特别指定package_dir(/ System/Library/...)”时。如果您使用2.6.4 python.org,则不应该有任何/ System/Library/Framework/Python ...路径。你是否为python.org python安装了单独版本的setuptools或Distribute,并且你是使用'easy_install'脚本(在/Library/Frameworks.../bin目录中)而不是Apple提供的easy_install在/ usr/bin(它将软件包安装到Apple提供的python中)? – 2010-02-02 07:22:55

1

由于评论没有很好的格式,因此

在find_modules中。PY我改变

REPLACEPACKAGES = { 
    '_xmlplus':  'xml', 
} 

REPLACEPACKAGES = { 
    #'_xmlplus':  'xml', 
} 

和XML导入工作。

+0

这现在被固定在最新modulegraph @ 0.8 0.2 – Steven 2011-01-10 18:12:16