tags:

views:

82

answers:

2

I'm trying to build an app that uses some xml data using Python's built-in xml.etree.ElementTree class. It works properly when I run from the command line, but when I build it, I get an error "ImportError: No module etree.ElementTree." I'm guessing this is because I'm not importing that module correctly, but I haven't been able to figure out how. When I use the "includes" or "packages" directive, py2app complains with the same error, and when I specifically specify the package_dir (/System/Library/...), it compiles, but still gives me the error. I've included a short example to illustrate the issue.

macxml.py

from xml.etree.ElementTree import ElementTree

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

This should print out "< Element Library at xxxxxx>" where Library is the root name.

setup.py

from setuptools import setup

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

What is the correct way to make the mac app utilize this library?

Python 2.6.4

Mac OS X 10.6.2

Edit: I also tried this on another mac (PPC 10.5.8) with Python 2.6.2 and achieved the same results.

A: 

If you're using macports (or fink etc.) make sure that py2app is using the correct interpreter. You may have to install a new version to work with 2.6.x (on OS X 10.5, py2app uses 2.4.x).

If that doesn't work my steps for working through path problems are:

  1. Start up the python interpreter that your code (or py2app) uses (are you absolutely certain?? try which python)
  2. import sys; print sys.path

If step 2. gives you a path in /System/Library..someotherpythonversion your code is running in the wrong interpreter.

Dana the Sane
I'm using the standard 2.6.4 install from python.org. The code seems to be running from the correct interpreter. Both `which python` and the sys.path point to `/Library/Frameworks/Python.framework/Versions/2.6/`.
jeffaudio
Must be some weird behaviour with `py2app` then, how did you install that? Have you tried running a script through `py2app` that prints the path?
Dana the Sane
You say "when I specifically specify the package_dir (/System/Library/...)". If you are using the 2.6.4 python.org, there should not be any /System/Library/Framework/Python... paths. Did you install a separate version of setuptools or Distribute for the python.org python and are you using that `easy_install` script (in the /Library/Frameworks.../bin directory) instead of the Apple-supplied easy_install in /usr/bin (which installs packages to the Apple-supplied python)?
Ned Deily
I don't remember how I installed py2app, but python shows it's located at `/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/py2app-0.4.2-py2.6.egg`. Also, the version of python I'm using is the Mac OS X dmg file from python.org (since my mac only had 2.6.3), but I noticed when I open it it says "Python 2.6.4 (r264:75821M, Oct 27, 2009, 19:48:23) [GCC 4.0.1 (Apple Inc. build 5493)] on darwin. setuptools shows I'm using v0.6c9-py2.6.egg. I will try reinstalling p2yapp, although I can't use easy_install from my current location b/c of the proxy.
jeffaudio
A: 

After reinstalling and updating macholib, modulegraph, py2app, and setuptools to no avail, I did a little more digging and found the following error in the modulegraph module:

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

So I looked more into the load_tail and import_hook functions and found that for some reason it was importing the xml package correctly, but then went to an old install of _xmlplus to look for the etree subpackage (which of course it couldn't find). Removing the _xmlplus package eliminated the error and I was able to get the application to work with the following setup.py file:

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'])]
     )

The output shows up in the console.

jeffaudio