ModuleNotFoundError: No module named ‘PIL’ in Python

Python supports multiple libraries that are used to deal with many simple to complex problems. To deal with images in Python, the library named “PIL” Python Image Library is used in a program. The “PIL” library was replaced by “pillow” after “PIL” was discontinued. To access this library/module, we need to install it in the Python environment. Otherwise, the “Module not Found Error” is invoked in the Python program.

This blog will address the reasons and solutions for the error “No module named PIL” in Python using the given below aspects:

Reason: PIL Module Not Installed in Python

The “No module named PIL” error occurs when the user tries to import the “pillow “ module without installing it in Python. The error also occurs when a user installs this module in an incorrect environment and tries to import it into another environment. The below example code invokes this error:

The above snippet shows that the “PIL” module is not installed on the system.

Solution 1: Install the PIL Module in Python (For Windows)

To resolve this “ModuleNotFoundError ” install the “pillow” module in Python. To install the “pillow” module follow the given below steps:

Step 1: Open Command Prompt Terminal

Open “cmd” by searching it from the search the

You can easily open the “cmd” terminal by following the above snippet.

Step 2: Install pillow Module

After opening the “cmd”, the next step is to uninstall the “PIL” module from Python. This is because both “PIL” and “pillow” modules can not co-exist simultaneously in the same environment. The “PIL” library was discontinued in “2011” and replaced by a new library named “pillow”. To uninstall the “PIL” library, type the command below in cmd:

> pip uninstall PIL

After uninstalling the “PIL” module now, we can install the latest “pillow” module using the given below command:

> pip install pillow

The above snippet shows that the “pillow” module version “9.3.0” was successfully installed in our system.

For due to some reason, if you get any permission error, then use the given below command:

> pip install Pillow --user

To install the “pillow” module in “Anaconda” and “Jupyter Notebook” use the given below command:

# for Anaconda
conda install -c conda-forge pillow

# Jupyter Notebook
!pip install Pillow

Step 3: Verification of pillow Module

To verify the installation of “pillow” module, use the following command:

> pip show pillow

The above snippet shows the name, location, and version of the installed “pillow” module in Python.

Solution 2: Install the PIL Module in Python (For Linux)

To install the “PIL” module in Python Linux, you can use the “pip” command in the terminal. Type the given below command in the terminal and press enter:

$ pip install pillow

Example: Opening Image Using pillow Module

To show the usage of the “pillow” module, the code below shows the working of how to open the image using the function of the “pillow” module:

Code:

from PIL import Image

img = Image.open("itslinuxfoss.png")

img.show()

In the above code, the “image.open()” function is used to open the image “itslinuxfoss.png” from the directory of Python. To show an image, the “img.show()” function is used at the end of the program.

Output:

The above snippet shows that the image “itslinuxfoss.png” is successfully displayed on the screen.

How to Uninstall pillow Module in Python?

To uninstall the “pillow” module in Python, use the given below command:

> pip uninstall pillow

The above snippet shows the “pillow” module is successfully uninstalled from Python.

Conclusion

The “ModuleNotFoundError: No module named PIL” error occurs in Python when a user tries to import a “pillow” module without installing it in Python. To resolve this error, uninstall the PIL module and Install the “pillow” module using the command “pip install pillow”. This Python blog delivered the reason and the solutions for the “No module named PIL” error in the program.