python3.5, tkinter and PIL - ImportError: cannot import name '_imagingtk'

python3.5, tkinter and PIL - ImportError: cannot import name '_imagingtk'

问题: 我怎样才能让 dpkg PIL/Pillow 版本与 python3.5 和 python3-tk 一起工作?

问题: 更新后重新启动系统后,调用 PIL 方法的 python3.5 tkinter 代码不再有效。错误信息是:

Traceback (most recent call last):
  File "/home/user1/.local/lib/python3.5/site-packages/PIL/ImageTk.py", line 176, in paste
    tk.call("PyImagingPhoto", self.__photo, block.id)
_tkinter.TclError: invalid command name "PyImagingPhoto"

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user1/Code/tkTreeview_test.py", line 37, in <module>
    app = App(root, path=path_to_my_project)
  File "/home/user1/Code/tkTreeview_test.py", line 22, in __init__
    self.root_pic2 = ImageTk.PhotoImage(root_pic1)      
  File "/home/user1/.local/lib/python3.5/site-packages/PIL/ImageTk.py", line 115, in __init__
    self.paste(image)
  File "/home/user1/.local/lib/python3.5/site-packages/PIL/ImageTk.py", line 180, in paste
    from PIL import _imagingtk
ImportError: cannot import name '_imagingtk'

阅读类似问题(例如 1, 2, 3)并查看错误信息后,我认为上述错误信息与 PIL.ImageTk 和我的 .[=42 中的 Tcl 之间的通信问题有关=].5/site-packages/PIL 文件夹。

我已使用 Synaptic Package Manager 清除 python3-pil、python3-pil.imagetk 和 python3-tk 并重新安装它们。另外,我尝试重新安装 tk8.6 和 tcl8.6。但是,每次 运行 代码通过 PIL.ImageTk 模块打开图像时,相同的错误消息仍然存在。

另外,我检查过我可以使用 python3-tk 中的 tkinter.PhotoImage(file=i) 打开图像文件。图片显示 w/o 任何错误消息。但是,我使用 PIL.ImageTk.PhotoImage(i) 打开相同的图像文件 i,我的代码失败并出现上述错误消息。

已安装的 dpkg 软件包:

ii  libtcl8.6:amd64             8.6.5+dfsg-2    amd64   Tcl (the Tool Command Language) v8.6 - run-time library files
ii  tcl                         8.6.0+9         amd64   Tool Command Language (default version) - shell
ii  tcl8.6                      8.6.5+dfsg-2    amd64   Tcl (the Tool Command Language) v8.6 - shell
ii  libtk8.6:amd64              8.6.5-1         amd64   Tk toolkit for Tcl and X11 v8.6 - run-time files
ii  tk8.6                       8.6.5-1         amd64   Tk toolkit for Tcl and X11 v8.6 - windowing shell
ii  tk8.6-blt2.5                2.5.3+dfsg-3    amd64   graphics extension library for Tcl/Tk - library
ii  python3-tk                  3.5.1-1         amd64   Tkinter - Writing Tk applications with Python 3.x
ii  python3-pil:amd64           3.1.2-0ubuntu1  amd64   Python Imaging Library (Python3)
ii  python3-pil.imagetk:amd64   3.1.2-0ubuntu1  amd64   Python Imaging Library - ImageTk Module (Python3)
ii  python3-pilkit              1.1.13+dfsg-1   all     Utilities and processors built for, and on top of PIL (Python3 version)

已安装的 pip 包: 使用 pip listpip3 list,我发现 pilkit ( 1.1.13) 和枕头 (3.2.0) 安装。这意味着我有 Pillow (3.2.0) 通过 pip 和 python3-pil:amd64 版本 3.1.2-0ubuntu1 通过 dpkg 安装在我的系统中。他们的共存是否会导致冲突,从而导致我的问题?

我被这个问题难住了,感谢有关克服上述错误消息的建议。谢谢

我通常使用 python 虚拟环境,当使用在包管理器之外安装的新包时 (synaptic/anaconda) 等

很简单,参考这个文档:
http://docs.python-guide.org/en/latest/dev/virtualenvs/

假设您的项目文件夹是 myProject

>> cd myProject
>> virtualenv venv
>> source venv/bin/activate
>> pip install package_name

回答:发现上面报错信息是使用Pillow(3.2.0)(即此时最新版本的PIL)与python3.5 和 tcl/tk 8.6。对于 PIL.ImageTk.PhotoImage() 方法,它以某种方式无法与 tcl 正确通信。查看我与@Akash 的通信,了解我是如何发现这一点的。

为了克服上面提到的错误消息,使 dpkg PIL/Pillow 版本 (3.1.2) 可以再次与 python3.5 和 python3-tk 一起工作,我做了以下:

  • 通过以下方式在主系统中卸载了 pip 安装的 Pillow (3.2.0) 命令行。

    对于 python3,卸载 Pillow(3.2.0) 的命令是:

    sudo pip3 uninstall Pillow==3.2.0 作为 sudo,或者

    pip3 uninstall Pillow==3.2.0 作为所有者。

    要在 python2.7 中卸载 Pillow(3.2.0),我只需 运行 相同的命令 但将 pip3 替换为 pip.

  • 为了检查 Pillow(3.2.0) 是否已卸载,我使用了 cmd pip3 list | grep Pillow 并出现了 Pillow (3.1.2)。我重新 运行 使用 PIL.ImageTk.PhotoImage() 方法的代码并且它们起作用了。