How to Fix "ModuleNotFoundError: No Module Named 'PIL'" in Python
Last Updated :
05 May, 2025
When working with images in Python, many developers use the Pillow library, a modern fork of the old PIL (Python Imaging Library), to handle image processing tasks. However, you may encounter the error ModuleNotFoundError: No module named 'PIL', which can prevent your script from running. This usually occurs when Pillow is not installed properly or the import statement is incorrect. Let’s explore the common causes of this error and how to fix them step by step.
Why Does ModuleNotFoundError: No Module Named 'PIL' Occur?
Let's understand the most common reasons for this error:
- PIL isn't installed: PIL is outdated and replaced by Pillow, which should be installed instead.
- Incorrect import: Use from PIL import Image instead of import PIL—Pillow uses the PIL namespace for modules.
- Virtual environment issues: Ensure Pillow is installed in your active environment if you're using one.
PIL vs. Pillow in Python
Understanding the difference between PIL and Pillow is important so you know which library to install and why pip install PIL won’t work. Let’s quickly compare them to clear up the confusion.
Feature | PIL | Pillow |
---|
Status | Deprecated (no longer maintained) | Actively maintained fork |
Installation | Not available via pip | Install with pip install Pillow |
Module Namespace | Uses PIL | Also uses PIL for backward compatibility |
Recommendation | Recommendation | Use Pillow |
How to Fix "ModuleNotFoundError: No Module Named 'PIL'"?
Let's understand a concise guide to resolving this issue:
1. Install Pillow: The most straightforward solution to the ModuleNotFoundError: No module named 'PIL' error is to install Pillow, which is the modern version of PIL. To install Pillow, open your terminal or command prompt and run the following command:
pip install Pillow
pip3 install Pillow
Once the installation is complete, Pillow will be ready to use.
2. Verifying the Installation: After installing Pillow, you can verify the installation by importing it in a Python script or an interactive Python session. Run the following commands to check if Pillow is correctly installed:
import PIL
print(PIL.__version__)
If the import is successful and the version number is printed, then Pillow is installed correctly.
3. Use the Correct Import Statement: If you're trying to use image-related features and wrote import PIL, it might not work as expected. Instead, you should import specific modules from Pillow like this:
from PIL import Image
from PIL import ImageDraw, ImageFont
Example:
Python
from PIL import Image
# Open an image file
img = Image.open('example.jpg')
img.show()
Output
example.jpg4. Check Python Path: If Pillow is installed but still shows an error, Python may not be looking in the right place. You can check the current Python path using:
import sys
print(sys.path)
This will list all directories Python is searching for modules. Make sure the folder where Pillow is installed is included in the list. If it’s not there, your environment might not be configured correctly.
5. Using a Virtual Environment: If you're using a virtual environment (which is great for keeping projects separate), remember that each environment needs its own packages. So, even if Pillow is installed globally, it won’t be available in your virtual environment unless you install it there. To activate your virtual environment:
source venv/bin/activate
venv\Scripts\activate
Then install Pillow in the environment:
pip install Pillow
6. Reinstall Pillow: If nothing else works, the installation might be broken or incomplete. Reinstalling usually fixes that. First, uninstall:
pip uninstall Pillow
Then, install it again:
pip install Pillow
Following these steps should help you fix the "ModuleNotFoundError: No module named 'PIL'" error and get Pillow working smoothly in your Python projects!
Related Articles
Similar Reads
How to Fix 'ModuleNotFoundError: No Module Named psycopg2' in Python
The psycopg2 package in Python interacts with PostgreSQL databases, providing an interface for executing SQL queries and managing database connections. If you encounter the 'ModuleNotFoundError: No Module Named psycopg2' error, it typically means that the package is not installed in your Python envi
2 min read
How to Fix - ModuleNotFoundError: No module named 'ipykernel' in Python
The Encountering the "No module named 'ipykernel'" error in Python can be frustrating especially when working with the Jupyter notebooks or JupyterLab. This error typically occurs when the 'ipykernel' module responsible for running Python code in Jupyter environments is missing or not configured cor
2 min read
How to Fix: ModuleNotFoundError: No module named 'tkinter'
On Ubuntu, while working with Python modules, you might encounter the "Tkinter Module Not Found" error. This typically happens because Tkinter is not installed by default with Python on some systems. Fortunately, resolving this issue involves a few simple steps to install the necessary Tkinter packa
2 min read
Modulenotfounderror: No Module Named 'CV2' in Python
If you are working with image processing using OpenCV becomes important for it. But sometimes we face a error saying: ModuleNotFoundError: No module named 'cv2' In this article we will see why it happens and how to fix it step by step. The "No module named 'cv2'" error is encountered in python when
2 min read
Modulenotfounderror: No Module Named Mysql in Python
When working with MySQL databases in Python, many developers use connectors like mysql-connector-python to interface with the database. However, the error ModuleNotFoundError: No module named 'mysql' can occur, preventing the successful execution of database operations. This error typically arises w
3 min read
How to Fix ModuleNotFoundError: No Module Named '_ctypes'
The ModuleNotFoundError: No Module Named '_ctypes' is a common error encountered in Python when the _ctypes module is not found. This module is essential for the many Python libraries and applications that rely on the C extensions for performance and functionality. This article will explain the prob
3 min read
How to Fix "No Module Named 'boto3'" in Python
The "No Module Named 'boto3'" error is a common issue encountered by Python developers working with the AWS services. The Boto3 is the Amazon Web Services (AWS) SDK for Python which allows the developers to interact with the various AWS services using the Python code. This error indicates that the B
3 min read
How to Fix "No Module Named 'xgboost'" in Python
The "No Module Named 'xgboost'" error is a common issue encountered by Python developers when trying to the use the XGBoost library a popular machine learning algorithm for the gradient boosting. This error typically occurs when the XGBoost library is not installed in the Python environment. In this
3 min read
How to Fix 'No Module Named yfinance' Error in Python
If you encounter the 'No Module Named yfinance' error, it typically means that the library is not installed or not accessible in your current Python environment. This issue can be resolved by ensuring that yfinance is properly installed and that your Python environment is configured correctly. What
3 min read
How to fix FileNotFoundError in Python
FileNotFoundError is a built-in Python exception that is raised when an operation such as reading, writing or deleting is attempted on a file that does not exist at the specified path. It is a subclass of OSError and commonly occurs when functions like open(), os.remove() and similar file-handling m
2 min read