Search code examples
pythonxmlpy2app

py2app and xml.etree.ElementTree


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.


Solution

  • 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.