ImportError: No module named PIL
On some installs of PIL, you must do
import Image
instead of import PIL
(PIL is in fact not always imported this way). Since import Image
works for you, this means that you have in fact installed PIL.
Having a different name for the library and the Python module is unusual, but this is what was chosen for (some versions of) PIL.
You can get more information about how to use this module from the official tutorial.
PS: In fact, on some installs, import PIL
does work, which adds to the confusion. This is confirmed by an example from the documentation, as @JanneKarila found out, and also by some more recent versions of the MacPorts PIL package (1.1.7).
No module named PIL' Visual Studio Code Error
This part is important:
If I run the file without vs code the module imports fine
If something like this happens, then you are not running the same python interpreter, because modules are always installed to specific installations of python that you have.
Do the following:
Add to your script the first two lines
import sys
print(sys.executable)
This will print the path to the python
executable that is interpreting that script. If you now run this script with and without vs code, it should print two different python paths. Now you can install to the python interpreter that is being used by vs code specifically by typing
/path/to/python/used/by/vs/code/python -m pip install pillow
No Module named PIL
As per my comment since it helped you out and answered your problem:
The issue that you were seeing is that you had pip version 1.5.6, and the version of pip does dictate how packages are unzipped, which ultimately determines whether or not modules are loaded properly.
All that is needed is:
pip install --upgrade pip
Which allows pip to upgrade itself.
Use sudo
if you're on Mac/Linux, otherwise you'll likely need to 'Run as Administrator' on Windows.
And voila, you can now properly import the PIL modules:
Python 2.7.12 (default, Jun 29 2016, 13:16:51)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import PIL
>>> from PIL import Image
>>> Image
<module 'PIL.Image' from '/usr/local/lib/python2.7/site-packages/PIL/Image.pyc'>
ModuleNotFoundError: No module named 'PIL' in Thonny
As you said
This issue occurs only in the Thonny IDE, not in the Python shell or IDLE
Thonny uses Python 3.7.9, whereas I use Python 3.9 otherwise
Your issue happens almost certainly because you installed Pillow for Python 3.9 and not for 3.7. Judging from Thonny's documentation on installing packages you should be able to open a shell and do
python3.7 -m pip install Pillow
and then you'll be able to import Pillow in Thonny as well.
You can confirm which Python version the pip3
command is using using pip3 --version
. For example on my Ubuntu system I have the system Python 3.8 and also Python 3.9, so I get this:
$ pip3 --version
pip 20.3.3 from /usr/local/lib/python3.9/dist-packages/pip (python 3.9)
$ python3.8 -m pip --version
pip 20.3.3 from /usr/local/lib/python3.8/dist-packages/pip (python 3.8)
from PIL import Image - ImportError: No module named PIL
I was experiencing a similar issue. Fixed it by pip installing WITHOUT sudo.
pip install pillow
instead of
sudo pip install pillow
Related Topics
Beautifulsoup Webscraping Find_All( ): Finding Exact Match
Parse HTML Table to Python List
How to Use the Python HTMLparser Library to Extract Data from a Specific Div Tag
Is There a Built in Package to Parse HTML into Dom
Paramiko Error When Trying to Edit File: "Sudo: No Tty Present and No Askpass Program Specified"
Arranging Text Files Side by Side Using Python
Threading in a Pyqt Application: Use Qt Threads or Python Threads
Too Many Values to Unpack', Iterating Over a Dict. Key=>String, Value=>List
Appending the Same String to a List of Strings in Python
Windows Scipy Install: No Lapack/Blas Resources Found
Keyboard Interrupts with Python's Multiprocessing Pool
How to Replace Text in a String Column of a Pandas Dataframe
How to Specify the Function Type in My Type Hints