Python 导入错误:无法使用 cx_Freeze 导入名称 MAXREPEAT
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16301735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ImportError: cannot import name MAXREPEAT with cx_Freeze
提问by GP89
I'm running into an issue with cx_Freezewhen running a frozen application (works fine unfrozen).
我在运行cx_Freeze冻结的应用程序时遇到了问题(解冻后工作正常)。
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 _srefrom a prompt works and I can access the MAXREPEATconstant.
我在 linux 上使用我从源代码构建的 python 2.7.4 版本,_sre从提示导入工作,我可以访问MAXREPEAT常量。
This is usually down to cx_Freezenot pulling everything into library.zipand 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.
这通常归结为cx_Freeze没有将所有内容都放入library.zip并且可以通过在cx_Freezes setup include list 中明确命名模块来修复,并且是这个类似问题的解决方案,但这在这里没有帮助。
This _sremodule seems weird.. there's no _srefile in the library.zipgenerated 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 _srestub doesn't have any imports.
这个_sre模块看起来很奇怪..生成的_sre文件中没有文件library.zip但是从那个错误中它似乎可以找到它,但是它不能导入那个符号?当然,如果模块不存在,它将是一个“ No module named _sre”错误。或者可能是循环导入但_sre存根没有任何导入。
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 _sremodule 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.
不返回任何内容,并且导入的_sre模块也没有__file__属性,因此我不知道如何确保包含它,因为它显示为内置模块。
>>> 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 questionalso 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.
这类似于最近也被问到的这个问题,但在这种情况下,他在常规解释器中遇到了错误,但对我来说,它只是在cx_Freeze.
edit
编辑
Running python -vdoes seem like it's a built-in, so I'm not sure why cx_Freezecan miss it, or how I'd fix it.
跑步python -v似乎是内置的,所以我不确定为什么cx_Freeze会错过它,或者我将如何修复它。
...
# /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
...
采纳答案by Thomas K
_sreis 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:
_sre是一个内置模块,因此没有要包含的文件,但它在 Python 2.7.3 中没有 MAXREPEAT 属性:
>>> 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.
我最好的猜测是,您的冻结副本以某种方式具有来自 Python 2.7.4 的标准库 .py 模块,但是来自 2.7.3 或更早版本的编译后的 Python 解释器。我看到您正在使用/usr/local- 也许它正在从/usr.
回答by Johnny Zhao
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
我刚从 ubuntu 12.10 升级到 13.04 时遇到了这个问题,我通过将 /usr/bin/python 复制到 /path/to/my/env/bin/ 来解决这个问题,它工作得很好
cp /user/bin/python /path/to/my/env/bin/
cp /user/bin/python /path/to/my/env/bin/
or, there's a more elegant way to fix this(reference):
或者,有一种更优雅的方法来解决这个问题(参考):
mkvirtualenv <existing virtualenv name>
mkvirtualenv <existing virtualenv name>
回答by OrhanC1
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
如果所有其他方法都失败了,我可以使用它运行:http: //www.kiwisoft.co.uk/blog/2014/08/17/fixed-importerror-cannot-import-name-maxrepeat
回答by Mikhail
I had the same problem recently. Setting LD_LIBRARY_PATH= solved the problem.
我最近遇到了同样的问题。设置 LD_LIBRARY_PATH= 解决了这个问题。
回答by PyBoy
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 Freezeif 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.
我在我的 win 8 机器上使用 cx_freeze 4.3.2,它总是显示ImportError: cannot import name MAXREPEAT with cx Freeze我是否曾尝试冻结非内置模块,一旦我下载了 4.3.1 版,它就可以工作,我可以冻结我的所有内容python 3.3程序现在没有任何问题。
回答by Benor
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:
我在 Windows 8 上遇到了类似的问题 - 只是 PYTHONPATH 问题。通过在 python 会话中键入以下内容来检查 PYTHONPATH 是否存在:
import os
os.environ['PYTHONPATH'].split(os.pathsep)
导入操作系统
os.environ['PYTHONPATH'].split(os.pathsep)
if you get an error set your PYTHONPATH using this approach..
如果出现错误,请使用此方法设置 PYTHONPATH。

