Search code examples
pythonpython-2.7cx-freeze

ImportError: cannot import name MAXREPEAT with cx_Freeze


I'm running into an issue with cx_Freeze when running a frozen application (works fine unfrozen).

When running the program it results in the following traceback:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/cx_Freeze/initscripts/Console.py", line 27, in <module>
    exec code in m.__dict__
  File "PythonApp/mainframe.py", line 3, in <module>
  File "/usr/local/lib/python2.7/site-packages/dbus/__init__.py", line 103, in <module>
    from dbus._dbus import Bus, SystemBus, SessionBus, StarterBus
  File "/usr/local/lib/python2.7/site-packages/dbus/_dbus.py", line 39, in <module>
    from dbus.bus import BusConnection
  File "/usr/local/lib/python2.7/site-packages/dbus/bus.py", line 39, in <module>
    from dbus.connection import Connection
  File "/usr/local/lib/python2.7/site-packages/dbus/connection.py", line 27, in <module>
    import threading
  File "/usr/local/lib/python2.7/threading.py", line 44, in <module>
    module='threading', message='sys.exc_clear')
  File "/usr/local/lib/python2.7/warnings.py", line 57, in filterwarnings
    import re
  File "/usr/local/lib/python2.7/re.py", line 105, in <module>
    import sre_compile
  File "/usr/local/lib/python2.7/sre_compile.py", line 14, in <module>
    import sre_parse
  File "/usr/local/lib/python2.7/sre_parse.py", line 17, in <module>
    from sre_constants import *
  File "/usr/local/lib/python2.7/sre_constants.py", line 18, in <module>
    from _sre import MAXREPEAT
ImportError: cannot import name MAXREPEAT

I'm on linux using a version of python 2.7.4 that I built from source, and importing _sre from a prompt works and I can access the MAXREPEAT constant.

This is usually down to cx_Freeze not pulling everything into library.zip and can be fixed by explicitly naming the module in cx_Freezes setup include list and is the solution to this similar question, but that hasn't helped here.

This _sre module seems weird.. there's no _sre file in the library.zip generated but from that error it seems like it can find it, however it can't import that symbol? Surely if the module wasn't there it would be a "No module named _sre" error. Or possibly a circular import but _sre stub doesn't have any imports.

What's odd is I can't seem to find the file either - is this module dynamically created when importing somehow?

find /usr/local/lib/python2.7 -name "_sre*"

doesn't return anything, and the imported _sre module doesn't have a __file__ attribute either, so I've no idea how to make sure it's included as it shows up as a built-in.

>>> import _sre
>>> _sre.__file__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__file__'
>>> repr(_sre)
"<module '_sre' (built-in)>"

This is similar to this question also which was asked recently, but in this case he was getting the error in the regular interpreter, however for me it's just in cx_Freeze.

edit

Running python -v does seem like it's a built-in, so I'm not sure why cx_Freeze can miss it, or how I'd fix it.

...
# /usr/local/lib/python2.7/re.pyc matches /usr/local/lib/python2.7/re.py
import re # precompiled from /usr/local/lib/python2.7/re.pyc
# /usr/local/lib/python2.7/sre_compile.pyc matches /usr/local/lib/python2.7/sre_compile.py
import sre_compile # precompiled from /usr/local/lib/python2.7/sre_compile.pyc
import _sre # builtin
# /usr/local/lib/python2.7/sre_parse.pyc matches /usr/local/lib/python2.7/sre_parse.py
import sre_parse # precompiled from /usr/local/lib/python2.7/sre_parse.pyc
...

Solution

  • _sre is a built in module, so there's no file to include for it, but it doesn't have a MAXREPEAT attribute in Python 2.7.3:

    >>> import _sre
    >>> _sre
    <module '_sre' (built-in)>
    >>> _sre.MAXREPEAT
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'module' object has no attribute 'MAXREPEAT'
    

    My best guess is that your frozen copy somehow has the standard library .py modules from Python 2.7.4, but the compiled Python interpreter from 2.7.3 or an earlier version. I see you're working from /usr/local - maybe it's picking up an older version from /usr.