15

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
...
Community
  • 1
  • 1
GP89
  • 6,600
  • 4
  • 36
  • 64

6 Answers6

32

I encountered this problem when I just upgraded from ubuntu 12.10 to 13.04, and I fixed this by copying the /usr/bin/python to /path/to/my/env/bin/, and it worked just fine

cp /user/bin/python /path/to/my/env/bin/

or, there's a more elegant way to fix this(reference):

mkvirtualenv <existing virtualenv name>

Community
  • 1
  • 1
Johnny Zhao
  • 2,858
  • 2
  • 29
  • 26
  • 6
    as suggested in the same referenced question, `virtualenv ` allows you to update the virtualenv without using virtualenvwrapper. – yoniLavi Jun 03 '13 at 17:49
  • mkvirtualenv is perfect, thanks. In case anyone is concerned about it destroying and other files in the virtualenv, it DOESN'T!! – hgolov Jun 23 '13 at 05:21
  • 1
    Upgraded from ubuntu 12.10 to 13.04 and faced the same above issue. Ran virtualenv on the existing virtualenv and this solved the issue. Thanks. – thiru_k Aug 18 '13 at 09:56
14

_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.

Thomas K
  • 39,200
  • 7
  • 84
  • 86
  • How does `cx_Freeze` find the interpreter to use? by looking in `$PATH`? I'll try printing out the python version number on the first line and see. – GP89 May 01 '13 at 09:34
  • You're spot on by the way :) `print sys.version` on the first line shows the interpreter in the frozen version is `2.7.3`. I guess the clue was that `_sre` is imported on interpreter start up in `2.7.4` but in my frozen application it was thrown later when I did `import dbus`. – GP89 May 01 '13 at 10:03
  • 1
    Ok, I thought I'd clear out the version of `2.7.3` so it wouldn't pick it up.. but it appears I don't have a version of `2.7.3`! I have `2.7.4` in `/usr/local` and `2.6.6` in `/usr`. Any idea of how this could be happening? – GP89 May 01 '13 at 10:07
  • Nothing springs to mind. You could try `locate libpython2.7` to look for other copies. Otherwise, try clearing out everything you can that's not essential for the system (i.e. don't remove the copy from /usr - most Linux systems use Python a lot), and then installing it afresh. – Thomas K May 01 '13 at 20:05
  • wiping python and re-installing did solve this, thanks. I think 2.7.3 was on there before and 2.7.4 must have installed over the top of it leaving some .3 bits lying about perhaps. Thank you for the clues! – GP89 May 08 '13 at 13:35
  • I upgraded to 2.7.5 and I've got the same issue (Not the MAXREPEAT error) but frozen apps with cx_Freeze are running 2.7.4 some how, my interpreter is 2.7.5 outside - so I don't think it's a virtualenv issue. (I recreated it without virutalenv as well). – GP89 Jul 09 '13 at 12:58
  • How did you upgrade? It must be finding the 2.7.4 Python shared library (.so file) somewhere. – Thomas K Jul 09 '13 at 20:42
  • @ThomasK Ah only seeing your reply now, I just did `python setup.py install` on the source I believe – GP89 Aug 28 '13 at 22:37
  • I think I meant how did you upgrade Python. But I don't know that I'll be able to offer much more advice anyway. – Thomas K Aug 28 '13 at 22:41
  • @ThomasK I didn't get notified you had replied again :P sorry! That is how I upgraded python (`python setup.py install` on the 2.7.5 source) – GP89 Oct 23 '13 at 19:23
  • @ThomasK, Where can i find that `_sre` file in python library.I have installed python(2.7) on my windows machine, And i found `sre.py` file in the `Lib` folder,But where is the `_sre` file in that..? – Shiva Sep 17 '14 at 07:10
  • @Shiva: there's no file for `_sre`, it's one of the modules that are built into the Python interpreter. – Thomas K Sep 17 '14 at 16:57
2

If all else fails, I got things running using this: http://www.kiwisoft.co.uk/blog/2014/08/17/fixed-importerror-cannot-import-name-maxrepeat

OrhanC1
  • 1,415
  • 2
  • 14
  • 28
1

I had the same problem recently. Setting LD_LIBRARY_PATH= solved the problem.

Mikhail
  • 11
  • 1
1

I was using cx_freeze 4.3.2 on my win 8 machine and it was always showing ImportError: cannot import name MAXREPEAT with cx Freeze if I ever tried to freeze a non built-in module, and once I downloaded version 4.3.1, it works, I'm able to freeze my all python 3.3 programs without any problem now.

zdd
  • 8,258
  • 8
  • 46
  • 75
Moin Khan
  • 63
  • 5
0

I was having similar issues on windows 8 - was just a PYTHONPATH issue. check that PYTHONPATH exists by typing the following into a python session:

import os

os.environ['PYTHONPATH'].split(os.pathsep)

if you get an error set your PYTHONPATH using this approach..

How to add to the pythonpath in windows 7?

Community
  • 1
  • 1
Benor
  • 1