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