rel*_*1kz 1 python pyinstaller attributeerror
我正在开发一个大型 python 项目,最后脚本由 PyInstaller 打包。上周我打包和运行我的应用程序没有遇到任何问题。这周我打包脚本时没有出现任何错误。然而,当我现在运行我的可执行文件时,我似乎遇到了一个相当普遍的错误,但来自一个奇怪的来源:
Traceback (most recent call last):
File "<string>", line 120, in <module>
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyi_importers.py", line 271, in load_module
exec(bytecode, module.__dict__)
File "/tmp/build/tmpDJZeLr/out00-PYZ.pyz/encodings", line 157, in <module>
AttributeError: 'module' object has no attribute 'register'
Run Code Online (Sandbox Code Playgroud)
我在我的代码中查找罪魁祸首,但我的项目中没有一个名为“注册”的属性/函数/等。我尝试在自己的代码中添加一些调试语句,但在弹出此错误之前,我的脚本中没有一个函数被调用。
然后我决定在 pyi_importers.py 中第 70 行附近添加一些打印语句:
sys.modules[fullname] = module
# Run the module code.
print module
print module.__dict__
print bytecode
exec(bytecode, module.__dict__)
Run Code Online (Sandbox Code Playgroud)
结果是:
<module 'encodings.aliases' from '/tmp/_MEIbDiUZq/encodings/aliases.pyc'>
{'__name__': 'encodings.aliases', '__file__': '/tmp/_MEIbDiUZq/encodings/aliases.pyc', '__loader__': <pyi_importers.FrozenImporter object at 0x7f1666fa0d10>, '__doc__': None, '__package__': 'encodings'}
<code object <module> at 0x7f1666fb4f30, file "/tmp/build/tmpg_2pIX/out00-PYZ.pyz/encodings.aliases", line 17>
Traceback (most recent call last):...
Run Code Online (Sandbox Code Playgroud)
我环顾四周,发现encodings.aliases 与内置的python 库有关。我正在使用 Python 2.7.6 和 PyInstaller 2.1;根据他们的文档,这应该是兼容的。我正在云基础 IDE (c9) 中工作,但似乎这不应该是问题,因为这是一个 python 而不是操作系统问题。
我很困惑。有什么帮助吗?
问题在于包名称冲突。我的项目中有一个名为“codecs”的包。当 PyInstaller 处理 python 标准库挂钩时,我的项目的命名空间胜过了 Python 标准库的命名空间。反过来,这导致encodings.aliases模块导入我的项目“codecs”模块而不是encodings模块。