How to fix ModuleNotFoundError: No Module Named ‘PIL’ In Python

If you get the error ModuleNotFoundError: No module named ‘PIL’ in Python and don’t know what to do. The cause of the error is that you have not installed the Pillow module so I have a few suggestions for installing the Pillow module for you. Read the article below.

What causes the ModuleNotFoundError: No module named ‘PIL’ error?

The ModuleNotFoundError: No module named ‘PIL’ error happens because you have not installed the ‘Pillow’ module to be able to use it or you installed the wrong environment. I will do the installation in a virtual environment.

How to solve this error?

Install library ‘PIL’ .

Install the ‘Pillow’ module. You can use Python pip. But note that you need to specify the exact version of Python you are using.

Example:

If you don’t have pip installed here is the solution for you in Python 3:

python -m pip install Pillow

If you use Python 3, use the pip3 statement as follows:

pip3 install Pillow

Install ‘Pillow’ on operating systems.

I would install ‘Pillow’ according to your operating system.

Install ‘Pillow’ on Windows using pip

Before installing ‘Pillow’, you need to create a virtual environment. Using a virtual environment helps you avoid incompatibilities between packages and break the system if you install incompatible libraries with your operating system.

You can activate the virtual environment with the following command:

venv\Scripts\Activate.ps1

Then you can install the ‘Pillow’ module with the following command:

python3 -m pip install Pillow

If you do not have pip installed, use the following command:

python3 -m pip install Pillow

Install ‘Pillow’ on Mac using pip

Make sure you have installed Python and pip. Just like on Windows operating systems, you also need a virtual environment. Just like on Windows operating systems, you also need your virtual environment. You can activate the virtual environment with the following command:

source venv/bin/activate

Install ‘Pillow’ in the command prompt environment:

python3 -m pip install Pillow

Install module ‘Pillow’ by Anaconda

Anaconda is the most popular open-source Data Science platform available on Python. Install Anaconda first, create a virtual environment, and install ‘Pillow’.

To create an anaconda environment:

conda create -n pil-env python=3.6

Finally, install ‘Pillow’ with the command:

conda install -c anaconda pillow

Import PIL module.

After you have installed the Pillow module, you can use it. Pillow is a fork from the PIL library with image processing functionality.

Example:

from PIL import Image

# Open and load images in your folder.
imageInfor = Image.open('the_cat.jpg')

# Parameters about the image by accessing its attributes.
print(imageInfor.format, imageInfor.size, imageInfor.mode)

Output:

JPEG (1000, 800) RGB

Summary

That’s how I figured out how to solve the error ModuleNotFoundError: No module named ‘PIL’ in Python. Hopefully, the article can help you. If you have any ideas or better ways, please comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *