Sun Bear
Sun Bear

Reputation: 8266

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

Question: How can I get dpkg PIL/Pillow version to work with python3.5 and python3-tk?

Problem: After rebooting my system after an update, my python3.5 tkinter codes which invokes PIL methods no longer works. The error message is:

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'

After reading up similar questions (e.g. 1, 2, 3) and looking at the error msg, I think the above error msg is related to communication problem between PIL.ImageTk and Tcl in my .local/lib/python3.5/site-packages/PIL folder.

I have used Synaptic Package Manager to purge python3-pil, python3-pil.imagetk and python3-tk and reinstall them again. Also, I have tried to reinstall tk8.6 and tcl8.6. However, the same error msg still persist every time a run a code opening images via the PIL.ImageTk module.

Separately, I have checked that I can use tkinter.PhotoImage(file=i) from python3-tk to open image files. Image showed up w/o any error msgs. However, I use PIL.ImageTk.PhotoImage(i) to open the same image file i, my code fails with the above error msg.

Installed dpkg packages:

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)

Installed pip packages: Using pip list and pip3 list, I found pilkit (1.1.13) and Pillow (3.2.0) installed. This mean I have Pillow (3.2.0) via pip and python3-pil:amd64 version 3.1.2-0ubuntu1 via dpkg install in my system. Is their coexistence causing a conflict and hence my problem?

I am stump by this problem and appreciate advice on overcoming the above mentioned error msg. Thanks

Upvotes: 0

Views: 7281

Answers (2)

Sun Bear
Sun Bear

Reputation: 8266

Answer: The error msg reported above is found to be unique to using Pillow(3.2.0) (i.e. the latest version of PIL at this time) with python3.5 and tcl/tk 8.6. It somehow fails to communicate with tcl properly for the PIL.ImageTk.PhotoImage() method. See my correspondence with @Akash to see how I discovered this.

To overcome the above mentioned error msg so that the dpkg PIL/Pillow version (3.1.2) can work with python3.5 and python3-tk again, I did the following:

  • Uninstalled the pip installed Pillow (3.2.0) in the main system via commandline.

    For python3, the command to uninstall Pillow(3.2.0) is either:

    sudo pip3 uninstall Pillow==3.2.0 as sudo, or

    pip3 uninstall Pillow==3.2.0 as owner.

    To uninstall Pillow(3.2.0) in python2.7, I simply ran the same commands but replace pip3 with pip.

  • To check that Pillow(3.2.0) was uninstalled, I used cmd pip3 list | grep Pillow and Pillow (3.1.2) appeared. I re-run my codes that used the PIL.ImageTk.PhotoImage() method and they worked.

Upvotes: 1

Akash Lodha
Akash Lodha

Reputation: 100

I generally use python virtual environment, when using new packages installed outside package managers(synaptic/anaconda) etc.

It's fairly simple, refer this doc :
http://docs.python-guide.org/en/latest/dev/virtualenvs/

Suppose your project folder is myProject

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

Upvotes: 0

Related Questions