Question

My goal is to create a python script that loops over cells of an excel document. This is my python script called reader.py, and it works just fine.

    import xlrd
    import os

    exceldoc = raw_input("Enter the path to the doc [C:\\folder\\file.xlsx]: ")

    wb = xlrd.open_workbook(exceldoc,'rb')
.... some code....

The problem I'm encountering is attempting to use py2exe to create an executable so this script can be used elsewhere.

Here is my setup.py file:

from distutils.core import setup
import py2exe
import sys
from glob import glob

setup(name='Excel Document Checker',console=['reader.py'])

I run the following command: python setup.py py2exe

It appears to run fine; it creates the dist folder that has my reader.exe file, but near the end of the command I get the following:

The following modules appear to be missing
['cElementTree', 'elementtree.ElementTree']

I did some searching online, and tried the recommendations here Re: Error: Element Tree not found, this changing my setup.py file:

from distutils.core import setup
import py2exe
import sys

from glob import glob

options={
    "py2exe":{"unbuffered": True,"optimize": 2,
   'includes':['xml.etree.ElementPath', 'xml.etree.ElementTree', 'xml.etree.cElementTree'],
 "packages": ["elementtree", "xml"]}}

setup(name='Excel Document Checker',options = options,console=['reader.py'])

I'm now getting an error: ImportError: No module named elementtree

I'm sort of at an impasse here. Any help or guidance is greatly appreciate.

Just some information - I'm running Python 2.6 on a 32 bit system.

Was it helpful?

Solution

You explicitly told setup.py to depend on a package named elementtree here:

"packages": ["elementtree", "xml"]}}

There is no such package in the stdlib. There's xml.etree, but obviously that's the same name.

The example you found is apparently designed for someone who has installed the third-party package elementtree, which is necessary if you need features added after Python 2.6's version of xml.etree, or if you need to work with Python 1.5-2.4, but not if you just want to use Python 2.6's version. (And anyway, if you do need the third-party package… then you have to install it or it won't work, obviously.)

So, just don't do that, and that error will go away.


Also, if your code—or the code you import (e.g., xlrd) is using xml.etree.cElementTree, then, as the py2exe FAQ says, you must also import xml.etree.ElementTree before using it to get it working. (And you also may need to specify it manually as a dependency.)

You presumably don't want to change all the third-party modules you're using… but I believe that making sure to import xml.etree.ElementTree before importing any of those third-party modules works fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top