Reputation: 419
when I run the following
from tkinter import *
from PIL import ImageTk, Image
root.mainloop()
I got
Traceback (most recent call last):
File "image_viewer.py", line 2, in <module>
from PIL import ImageTk, Image
ImportError: No module named PIL
but I already install Pillow and everything is fine.
Upvotes: 39
Views: 256706
Reputation: 21
The problem is that you are not running the same python interpreter. In addition, the problem could arise from this fact that python cannot find the path to the OpenCV.
You first need to find the path to the OpenCV installed on your OS.
First, open a python script and run this:
import cv2
PATH = cv2.__file__
print(PATH)
This will print the PATH to the OpenCV installed on your OS.
Then, add the following two lines to the top of your main script (before calling tkinter and PIL):
import sys
sys.path.append('PATH')
from tkinter import *
from PIL import ImageTk, Image
root.mainloop()
Alternative Solution:
The problem could be that you are not running the same python interpreter.
You first need to find the path to the python executable that is interpreting your python scripts.
Open a python script and run this:
import sys
PATH = sys.executable
print(PATH)
This will print the path to the python executable that is interpreting your python scripts.
Now, you can install pillow in the found path as follows:
PATH -m pip install pillow
Upvotes: 1
Reputation: 64
If you are sure that you already installed pillow use this command pip install pillow --upgrade
, then you can use the command pip freeze
to list all modules that have been installed.
Upvotes: 0
Reputation: 12189
Use Pillow which is the "new" or the replacement of PIL, but has the same-named modules to preserve compatibility:
pip install pillow
Also, as suggested in the comments, maybe you are just using the wrong python binary, try to check if you're in/out of a virtual environment or check differences between python
vs python3
vs python2
on your system:
python -m pip list
python2 -m pip list
python3 -m pip list
Upvotes: 64