【问题标题】:"ImportError: cannot import name _imaging" after using PyInstaller (Python 2)使用 PyInstaller (Python 2) 后出现“ImportError: cannot import name _imaging”
【发布时间】:2014-07-17 07:43:05
【问题描述】:

我使用 Python 2 编写的代码在从 Python 运行时运行良好,但在我通过 PyInstaller 发送脚本并运行可执行文件后,它会吐出:

Traceback (most recent call last):
  File "<string>", line 26, in <module>
  File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyi_importers.py", line 270, in load_module
    exec(bytecode, module.__dict__)
  File "/home/ben/Documents/Programming/Python/Weasyl/Test Scripts/build/test3/out00-PYZ.pyz/PIL.PngImagePlugin", line 40, in <module>
  File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyi_importers.py", line 270, in load_module
    exec(bytecode, module.__dict__)
  File "/home/ben/Documents/Programming/Python/Weasyl/Test Scripts/build/test3/out00-PYZ.pyz/PIL.Image", line 53, in <module>
ImportError: cannot import name _imaging

我不完全确定为什么在导入这个模块时会出现问题,因为 PyInstaller 将 Tkinter 和 PIL 列为兼容的。整个代码如下:

#!/usr/bin/env python

from Tkinter import *
from PIL import ImageTk, Image
import os

root = Tk()
name = Label(root, text="(username)", font="Arial 20")
name.grid(row=0, column=0)
status = Label(root, text="(login status)")
status.grid(row=1, column=0)
img = ImageTk.PhotoImage(Image.open(".avatar.png"))
panel = Label(root, image=img, relief=RAISED, height=100, width=100)
panel.grid(row=0, column=1, columnspan=2, rowspan=2)
root.mainloop()

我假设问题出在from PIL import ImageTk, Image 上,但没有办法使用该模块,因为我要使用的图像是 PNG 格式,据我所知,Tkinter 仅支持 GIF 格式之外使用那个模块。

编辑:如果我不能将此模块与 PyInstaller 一起使用,有没有办法让 PNG 图像在此模块之外的 Tkinter 中显示?

【问题讨论】:

    标签: python tkinter python-imaging-library pyinstaller


    【解决方案1】:

    我正在努力解决类似的问题。 PIL 模块 Imaging 不起作用,尽管 PyInstaller 似乎没有抱怨。 但是,它只是在我更新到 Linux Mint 17 之后才发生在我身上。之前使用过 Mint 15,并且程序在那里编译并运行良好。

    我想我会尝试旧版本的 PIL 或其他加载纹理的方法...

    更新: 好的,好消息! 似乎问题已在 PyInstaller 的当前开发版本中得到修复。可以在这里下载: https://github.com/pyinstaller/pyinstaller

    我还尝试将所有纹理代码更改为使用 PyGame 而不是 PIL(因为无论如何我在我的程序中使用它),但是在使用 PyInstaller 2.1 制作可执行文件后运行 PyGame 也有问题。

    但是,使用来自 GitHub 的 PyInstaller 的当前开发版本,PyGame 和 PIL 都可以正常工作!

    【讨论】:

    • 这并不能真正回答问题。如果您有其他问题,可以点击 提问。一旦你有足够的reputation,你也可以add a bounty引起更多的关注。
    • @Hasturkun - 我不认为这是在问另一个问题。他提到有一个类似的问题,但后来通过使用不同的版本来解决它。我认为删除它的最佳理由是因为它更多的是旁白或评论,而不是对这个问题的回答。
    • @ArtOfWarfare:这不是答案。它应该是评论或单独的问题。我刚刚为这个案例挑选了最接近的股票评论。
    • 好的,我已经设法让我的程序现在可以运行并更新了帖子。 :)
    • @XArgon 如何安装开发版?
    猜你喜欢
    • 2015-02-14
    • 1970-01-01
    • 2018-10-27
    • 2018-11-13
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多