How to Resolve Python Error "ModuleNotFoundError: No module named 'PIL'"
When working with image processing in Python, you'll almost certainly use the Pillow library, the actively maintained fork of the original Python Imaging Library (PIL).
A common point of confusion leads to the ModuleNotFoundError: No module named 'PIL'.
This error arises because while you import PIL (or specific modules like from PIL import Image) in your code, the package you need to install is actually named Pillow.
This guide explains this naming convention and provides comprehensive steps to install Pillow correctly and resolve the import error.
Understanding the Error: PIL vs. Pillow
- PIL (Python Imaging Library): The original image processing library for Python. It is largely unmaintained and does not support modern Python versions well.
- Pillow: An actively developed and maintained fork of PIL. It's designed as a drop-in replacement for PIL, offering the same functionality plus many improvements and compatibility with current Python versions.
- The Confusion: To maintain backward compatibility, Pillow keeps the original import namespace. This means you install
Pillowbut importPILin your code.
The ModuleNotFoundError: No module named 'PIL' occurs because Python cannot find the PIL namespace, which is provided only when the Pillow package is correctly installed in the active environment.
Common Causes
PillowNot Installed: ThePillowpackage was never installed.- Old
PILInstalled: An outdated, incompatible version of the originalPILmight be installed, potentially conflicting withPillow. - Incorrect Environment:
Pillowwas installed in a different Python environment (e.g., global) than the one running the script (e.g., a virtual environment). - Virtual Environment Not Activated: Forgetting to activate the virtual environment before installing or running.
- IDE Misconfiguration: VS Code, PyCharm, etc., are pointing to the wrong Python interpreter.
- Filename Shadowing: Having a local file named
PIL.pyorpil.py.
Solution 1: Install the Pillow Package (pip/conda)
This is the primary solution. Ensure you are in the correct environment (activate your virtual environment if using one).
Uninstall Old PIL (Important Prerequisite)
Crucially, Pillow and the original PIL cannot coexist reliably. Before installing Pillow, explicitly uninstall any old PIL versions:
pip uninstall PIL
pip uninstall Pillow # Also uninstall Pillow if retrying installation
# Use pip3, python -m pip etc. as needed for your environment
You might get a "not installed" message, which is fine.
Install Pillow
Now, install the correct package:
- Using
pip:pip install Pillow
# Or more explicitly
python -m pip install Pillow
pip3 install Pillow
python3 -m pip install Pillow
py -m pip install Pillow # Windows
# If permission issues occur (less likely in venv)
pip install Pillow --user
# sudo pip3 install Pillow # Avoid if possible - Using
conda(Anaconda):# Anaconda channel often has it, or use conda-forge
conda install -c anaconda pillow
# or sometimes just: conda install pillow
After installation, your import PIL or from PIL import Image statements should work.
Solution 2: Verify the Python Environment
Ensure consistency between installation and execution environments.
-
Checking Python Version: Use
python --version/pip --version. Pillow supports a wide range of modern Python 3 versions. -
Using Virtual Environments (Recommended):
- Create:
python3 -m venv venv - Activate:
source venv/bin/activate(Linux/macOS) orvenv\Scripts\activate(Windows) - Uninstall old PIL (while active):
pip uninstall PIL - Install Pillow (while active):
pip install Pillow - Run script (while active).
- Create:
-
Checking IDE Interpreter Settings: Configure VS Code ("Python: Select Interpreter") or PyCharm (
File>Settings>Project>Python Interpreter) to use the Python interpreter from within your virtual environment. -
Note on VS Code Pylance Error (
Import "PIL" could not be resolved) Similar to the runtime error, this static analysis error from Pylance means it can't find thePILmodule source in the currently selected VS Code interpreter. The solutions are the same:- Uninstall
PILif it exists (pip uninstall PILin the venv terminal). - Install
Pillow(pip install Pillowin the venv terminal). - Ensure VS Code has the correct virtual environment interpreter selected.
- Restart VS Code often helps Pylance recognize the change.
You can use
# type: ignoreon the import line as a last resort, but it's strongly discouraged as it hides the underlying problem.
- Uninstall
Solution 3: Check for Filename Shadowing (PIL.py / pil.py)
Do not name any of your project files PIL.py or pil.py. This will interfere with the real import. Rename any such files.
Debugging Steps
- Check Installation (
pip show Pillow): Activate your environment. Runpip show Pillow. If found, check theLocation. If not found, it needs installing. Do not usepip show PIL. - Restart IDE / Kernel / Script: Close and reopen VS Code/PyCharm. Restart Jupyter kernels. Stop/start your script.
- Reinstall / Upgrade Package:
pip uninstall PIL # Ensure old one is gone
pip uninstall Pillow
pip install Pillow
# Or upgrade
pip install --upgrade Pillow
Platform/Tool Specific Installation Guides
These sections guide opening terminals/using tools to run the essential commands: pip uninstall PIL followed by pip install Pillow or the conda equivalent.
- Install Pillow on Windows: Open Command Prompt/PowerShell (maybe as Admin). Run
pip uninstall PIL, thenpip install Pillow(using specificpip/python -m pip/py -m pipas needed). Usevenv. - Install Pillow on macOS or Linux: Open Terminal. Run
pip uninstall PIL, thenpip3 install Pillow(or specificpython3 -m pip). Usevenv. Consider--userorsudoonly if necessary. - Install Pillow in Visual Studio Code: Use integrated terminal (ensure correct venv). Run
pip uninstall PIL, thenpip install Pillow. Select correct interpreter. - Install Pillow in PyCharm: Use terminal panel (check venv) or UI package installer (
Settings>Interpreter>+). Runpip uninstall PIL, thenpip install Pillowin terminal, OR search/installPillowvia UI. Set correct project interpreter. - Install Pillow in Anaconda: Activate conda env. Use
conda install -c anaconda pillow(conda usespillowname). Or, within the conda env, usepip uninstall PILthenpip install Pillow. - Install Pillow in Jupyter Notebook: Best: Install in terminal (activated env) before starting Jupyter. Alt: In cell, run
!pip uninstall PIL -y && !pip install Pillow. Restart Kernel afterwards.
Conclusion
The ModuleNotFoundError: No module named 'PIL' arises from the historical split between the original PIL library and its modern, maintained fork, Pillow.
To fix this:
- Uninstall any old
PILpackage:pip uninstall PIL. - Install the correct package:
pip install Pillow(orconda install pillow). - Ensure installation occurs within the correct, active Python environment (use virtual environments).
- Configure your IDE to use that same environment's interpreter.
- Use
import PILorfrom PIL import ...in your Python code.
Following these steps correctly aligns the installed package (Pillow) with the imported namespace (PIL), resolving the error.