我在运行包含枕头的已编译可执行文件时遇到问题。我使用了 pyinstaller,它似乎编译得很好,但是运行 exe 后,会抛出错误。似乎与引用“_imaging”而不是“Image”存在冲突。
首先,我觉得很奇怪pillow如何安装到PIL目录而不是pillow,并且模块也被引用为PIL。但我怀疑这与这个问题有什么关系。
然而,Python27\Lib\site-packages\PIL 中实际上并没有 _imaging.py 文件,我怀疑这可能是一个问题,因为 pyinstaller 使可执行文件需要它。不确定是否有某种方法可以将其从构建中排除。这可能是下一步。
诸如此类的问题this https://stackoverflow.com/questions/30203575/import-error-when-running-pyinstaller-app and this https://stackoverflow.com/questions/27503961/error-using-pillow-importerror-cannot-import-name-imaging已提出但未解决。有些人还提到在安装pillow之前未卸载PIL,但PIL肯定已卸载,并且我尝试了pillow的二进制文件和pip安装。
有什么建议么?
运行时错误:
Traceback (most recent call last):
File "<string>", line 26, in <module>
File "C:\Python27\Lib\site-packages\PyInstaller\loader\pyi_importers.py", line 270, in load_module
exec(bytecode, module.__dict__)
File "C:\Users\Hp\PycharmProjects\GISdev\build\gis_helper2\out00-PYZ.pyz\PIL.PngImagePlugin", line 40, in <module>
File "C:\Python27\Lib\site-packages\PyInstaller\loader\pyi_importers.py", line 270, in load_module
exec(bytecode, module.__dict__)
File "C:\Users\Hp\PycharmProjects\GISdev\build\gis_helper2\out00-PYZ.pyz\PIL.Image", line 63, in <module>
ImportError: cannot import name _imaging
下面是 PIL.Image 文件的摘录,有问题的行是
from PIL import _imaging 作为核心
try:
# If the _imaging C module is not present, Pillow will not load.
# Note that other modules should not refer to _imaging directly;
# import Image and use the Image.core variable instead.
# Also note that Image.core is not a publicly documented interface,
# and should be considered private and subject to change.
from PIL import _imaging as core
if PILLOW_VERSION != getattr(core, 'PILLOW_VERSION', None):
raise ImportError("The _imaging extension was built for another "
" version of Pillow or PIL")
from PIL import Image
这就是我在脚本中调用枕头的方式:
from PIL import Image