ImportError: cannot import name MAXREPEAT with cx_Freeze

无人久伴 提交于 2019-12-18 05:39:26

问题


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

回答1:


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




回答2:


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>




回答3:


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




回答4:


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




回答5:


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.




回答6:


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?



来源:https://stackoverflow.com/questions/16301735/importerror-cannot-import-name-maxrepeat-with-cx-freeze

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!