Py2exe lxml woes

Question:

I have a wxpython application that depends on lxml and works well when running it through the python interpreter. However, when creating an exe with py2exe, I got this error

ImportError: No module named _elementpath

I then used python setup.py py2exe -p lxml and I did not get the above error but
another one saying

ImportError: No module named gzip

Could anyone let me know what the problem is and how I can fix it. Also should I put any
dll files like libxml2, libxslt etc in my dist folder? I searched the computer
and did not find these files, so maybe they aren’t needed?

Thanks.

Edit: I just tried with python setup.py py2exe -p -i gzip and the exe was created. But the exe generated does not run. I double click it and it doesn’t do anything.

Here’s the setup.py script i’m using

from py2exe.build_exe import py2exe
from distutils.core import setup

setup( windows=[{"script": "gui.py"}] )

Edit2: I tried using cx_freeze as an alternative , but got the same

ImportError: No module named _elementpath

error. Didn’t know how to proceed after that.

Asked By: jack the lesser

||

Answers:

Py2exe allows you to specify additional packages/modules to include with the options argument to setup(), in case they are not automatically detected. The following should work:

from distutils.core import setup
import py2exe

setup(
    windows=[{'script': 'gui.py'}],
    options={
        'py2exe': 
        {
            'includes': ['lxml.etree', 'lxml._elementpath', 'gzip'],
        }
    }
)

I’ve also recently discovered PyInstaller, which has built-in support for a number of well-known packages, including lxml, so that might be worth a try as well.

Answered By: Steven

Sometimes you will need to do another action more after modify the setup.py file.

As described here,
it should be necessary to uninstall the package if it is installed on “eggs” archive.
Then install it again by forcing easy_install to dezip the archive with the “-Z” option as following (I had the issue with paramiko package):

pip uninstall paramiko
easy_install -Z paramiko
Answered By: Progsam
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.