Install or reinstall the maintained pillow package into the exact Python interpreter running your script via python -m pip install --upgrade pillow (never pip install PIL), while keeping your import statement as from PIL import Image—and remove any conflicting legacy PIL package or local file named PIL.py if the error persists.
Basic Example
# 1. Recommended: Install Pillow into the active Python interpreter's environment
python -m pip install --upgrade pillow
# Windows (if `python` points to the wrong installation):
py -m pip install --upgrade pillow
# 2. Verify both the interpreter path and that Pillow imports cleanly:
python -c "import sys, PIL; print(sys.executable, PIL.__version__)"
# 3. Always import from `PIL` (capitalized), NOT `import pillow`
from PIL import Image
img = Image.new("RGB", (100, 100), color="steelblue")
print(img.size) # Output: (100, 100)
Key Details & Edge Cases
- Package name vs. import name (
pip install pillowvs.from PIL import Image):- The original
PIL(Python Imaging Library) was discontinued in 2009 and does not support Python 3—runningpip install PILfails withCould not find a version that satisfies the requirement PIL. - Pillow is the drop-in fork installed under the PyPI package name
pillow, while keepingPILas its top-level import namespace (import pillowraisesModuleNotFoundError: No module named 'pillow').
- The original
- Interpreter / virtual environment mismatch (
pipvs.python -m pip):- The most common cause of
No module named 'PIL'after installing Pillow is having multiple Python installations (system Python, Homebrew, Conda,.venv, or VS Code / Jupyter kernels) where barepip install pillowinstalls into a different environment thanpython script.py. - Always run
python -m pip install pillow(or select the matching.venvinterpreter in VS Code / Jupyter) sopiptargets the exact same environment aspython.
- The most common cause of
- Conflicting broken
PILinstall or file shadowing:- If an old
PILdirectory exists alongsidepillowinsite-packages, uninstall both and reinstall Pillow cleanly:python -m pip uninstall -y PIL pillow python -m pip install pillow - Also verify you have not named your own script or folder
PIL.pyorpillow.pyin the working directory, which shadows the installed library.
- If an old
- Conda / Mamba environments:
- If you use Anaconda or Miniconda, install via
conda install -c conda-forge pillow(ormamba install pillow) inside your activated Conda environment so its compiled C libraries (libjpeg,zlib,libtiff) match your Conda runtime.
- If you use Anaconda or Miniconda, install via
Comparison of Pillow Installation & Verification Commands
| Environment / Scenario | Command | Import Syntax | Best Used When |
|---|---|---|---|
Standard venv / pip (Recommended) |
python -m pip install --upgrade pillow |
from PIL import Image |
Installing Pillow into your active virtual environment or default Python interpreter. |
| Windows Python Launcher | py -m pip install --upgrade pillow |
from PIL import Image |
Windows systems with multiple Python versions installed globally. |
| Conda / Miniconda | conda install -c conda-forge pillow |
from PIL import Image |
Managing environments via Conda where native C imaging codecs must match Conda binaries. |
Clean Reinstall (Corrupted PIL) |
python -m pip uninstall -y PIL pillow && python -m pip install pillow |
from PIL import Image |
Resolving broken site-packages leftovers or partial wheel installations. |
You must log in or register to comment.
