No Module named PIL

Question:

I know PIL is deprecated, so I installed Pillow instead, and for backward compatibility reason, Pillow is still using PIL as its module name.
Here’s my pip freeze look like for Pillow:
Pillow=3.2.0

Here’s how I use it in my code:

import  PIL as pillow
from PIL import Image

As someone suggested in a similar post, also, I tried

import  PIL 
from PIL import Image

Neither works.
Both give me this error:

ImportError: No module named PIL

Also, I tried:

import Image

it gives me:

ImportError: No module named Image

Help is really appreciated!

Asked By: Fisher Coder

||

Answers:

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'>
Answered By: Signus

In my case, on Windows, all I needed to do is to run:

pip install pillow

Answered By: Arthur

You can use python -m pip install Pillow or pip install Pillow

Answered By: LF00

I’m having this trouble too recently, so let me share my take on this. I’m on Mac OS X Mojave 10.14. I tried running the pip install pillow so many times blindly without finding deeper understanding as to where my python searches for its packages.

I encountered this error when I upgraded from OS X High Sierra to Mojave. My previously working Scrapy project just stopped running properly.

Note that I didn’t use virtualenv.

First, you need to check which python is used by the system. In the Terminal, run

which -a python

You’ll get something similar to this:

nicholass-mbp:~ nicholaslie$ which -a python
/usr/local/bin/python
/usr/local/bin/python
/usr/bin/python

This leads me to searching inside the /usr/local/ folder. Turns out there’s a folder to where my python is searching its packages, and that is inside /usr/local/lib/python2.7 (python) or /usr/local/lib/python3.7 (python3)

What my current pip install pillow do is installing the PIL package inside the /usr/local/lib/python2.7/site-packages directory, but not to /usr/local/lib/python3.7/site-packages

PIL in /usr/local/lib/python2.7/site-packages

However, since my project uses Python 3, No module named PIL error simply tells you that the python3 instance you’re using cannot find the PIL library.

I used Homebrew for managing my system packages such as node, python, etc. So what I did was reinstall python3 with Homebrew with:

brew reinstall python, then run pip3 install pillow.

Just ensure that the PIL package is installed properly in /usr/local/lib/python3.7/site-packages and you should be good to go. Hope this helps someone.

Answered By: Nicholas Lie

OSX

sudo easy_install pip
pip install Pillow --user

enter image description here

For me, when I was trying to use PIL in PyCharm, initially the Project Interpreter was not looking at the correct location for Python 3.7. This has been corrected and using PIL works for me now (corrected Interpreter shown in image attached).

Answered By: Baba.S

This solution is for pip version 20 and using PyCharm.
I’d upgrade pip to version 20.0.2 as pip suggested but I had to get back to version 19

python -m pip install pip==19.3.1

I also installed Pillow from command line

pip install Pillow

And finally installed Pillow inside PyCharm on Settings -> Project:your-project-name -> Project Interpreter, clicked on + button and installed Pillow.

Pillow installed from inside MyCharm

Answered By: user3551863
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.