Why am I getting this ImportError?

Zach Gates picture Zach Gates · Mar 2, 2014 · Viewed 9.1k times · Source

I have a tkinter app that I am compiling to an .exe via py2exe.

In the setup file, I have set it to include lxml, urllib, lxml.html, ast, and math.

When I run python setup.py py2exe in a CMD console, it compiles fine. I then go to the dist folder It has created, and run the .exe file.

When I run the .exe I get this popup window.this
(source: gyazo.com)

I then procede to open the Trader.exe.log file, and the the contents say the following;

Traceback (most recent call last):
  File "Trader.py", line 1, in <module>
  File "lxml\html\__init__.pyc", line 42, in <module>
  File "lxml\etree.pyc", line 12, in <module>
  File "lxml\etree.pyc", line 10, in __load
  File "lxml.etree.pyx", line 84, in init lxml.etree (src\lxml\lxml.etree.c:190292)
ImportError: cannot import name _elementpath

Included here is a copy of my setup.py file.

Please help me find the problem here. Thanks in advance.

Answer

anon582847382 picture anon582847382 · Mar 2, 2014

Looks like py2exe doesn't realize it should include the lxml._elementpath module, which is conditionally imported by lxml.etree. You can tell it to include that module explicitly with the includes keyword argument in your setup.py.

setup(
    options={'py2exe': {"includes": ["lxml._elementpath"]}}
)