Search code examples
pyqtlxmllibxml2cx-freeze

CxFreeze is not recognizing certain imports


After building my executable with CX_Freeze and trying to run the .exe I get this error. I understand this means CxFreeze is not recognizing lxml. However I have tried to include this in my setup.py

Traceback(most recent call last):
   File "C:\Python27\lib\site-packages\cx_Freeze\initscripts\Console.py",
line 27, in <module>
    exec(code, m._dict_)
   File "mainFile.py", line 2, in <module>
   File "C:\Users\user\TestFile\testPyQt.py",line 11, in
  <module>
     import myFile
   File "C:\Users\user\TestFile\myFile.py", line 4, in 
  <module>
    from lxml import etree
   File "ExtensionLoader_lxml_etree.py", line 22, in <module>
   File "ExtensionLoader_lxml_etree.py", line 14, in _bootstrap_
   file "lxml.etree.pyx", line 84, in init lxml.etree
  (src\lxml\lxml.etree.c:191837)
   ImportError:cannot import name_elementpath

My setup.py has this:

INCLUDE_MODULES = [

    'lxml',
    'xml.etree.ElementTree',
    'xml.etree.ElementPath',
    'libxml2'
]

Solution

  • It looks a little like the error message is missing a space. I'm pretty sure it is complaining that `lxml._elementpath' cannot be imported. This is because that module is conditionally imported by lxml.

    The solution is to add lxml._elementpath to your list of INCLUDE_MODULES.

    Note, this solution effectively comes from these sources, where people had similar problems with py2exe or other packages with cx_freeze:

    Why am I getting this ImportError?

    cx_Freeze ImportError: cannot import name