0

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

Community
  • 1
  • 1
Sun Bear
  • 7,594
  • 11
  • 56
  • 102
  • try this `sudo apt-get install python-imaging-tk` – shivsn Jun 03 '16 at 06:53
  • I am using python3.5 should I instead use sudo apt-get install python3-imaging-tk? On my system, python defaults to python2.7. – Sun Bear Jun 03 '16 at 07:19
  • @shivsn Note, selecting 'python-pil.imagetk' instead of 'python-imaging-tk' python-pil.imagetk is already the newest version (3.1.2-0ubuntu1). 0 to upgrade, 0 to newly install, 0 to remove and 0 not to upgrade. No improvement to my problem. – Sun Bear Jun 03 '16 at 07:34

2 Answers2

1

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.

Sun Bear
  • 7,594
  • 11
  • 56
  • 102
  • Should have done it already but thanks for the answer. I was searching for solution for more than 2 hours and you sir saved my day :P – Shashank Jul 03 '17 at 09:29
0

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
Akash Lodha
  • 100
  • 9
  • Do you mean I should pip3 uninstall pilkit (1.1.13) and Pillow (3.2.0) via my terminal first before installing it in virtualenvs? – Sun Bear Jun 03 '16 at 06:19
  • no you donot need to unistall. Just read the documnet once, you will be clear with the virtual environment concept. – Akash Lodha Jun 03 '16 at 06:27
  • for your project create a virtual environment. Install all the required packages using pip in the virtual environment. – Akash Lodha Jun 03 '16 at 06:29
  • Thanks. I do understand the use of virtualenv. However, it does not remedy the failure I am having with PIL, python3.5 and tkininter in main system setup. I still need to get that working. Any idea how to? Btw, I did try to uninstall the Pillow using pip but I could not. – Sun Bear Jun 03 '16 at 06:55
  • I have tried to run the same code in the virtualenv folder that I created and unfortunately got the same error. – Sun Bear Jun 03 '16 at 07:15
  • I was curious if the error msgs I got is unique to Pillow 3.2.0. To investigate this, I replace it will its older version (i.e. Pillow 3.1.2) in my virtualenv folder. To my pleasant surprise, my test code worked again. The test code I use can be found [here](http://stackoverflow.com/questions/37549976/tkinter-ttk-treeview-root-node-icon-image-does-not-appear/37551858#37551858). This means there is a bug in PILLOW 3.2.0. I have tried this a few times to check my finding. If you have time can you let me know if you have the same issue using PILLOW 3.2.0 vs 3.1.2? – Sun Bear Jun 03 '16 at 08:20