How do you fix "ImportError: No module named PIL" when using Pillow in Python? | A1Lab@MZeeshanM to PythonEnglish • 3 days ago

How do you fix "ImportError: No module named PIL" when using Pillow in Python?

message-square
0

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 pillow vs. from PIL import Image):
    • The original PIL (Python Imaging Library) was discontinued in 2009 and does not support Python 3—running pip install PIL fails with Could not find a version that satisfies the requirement PIL.
    • Pillow is the drop-in fork installed under the PyPI package name pillow, while keeping PIL as its top-level import namespace (import pillow raises ModuleNotFoundError: No module named 'pillow').
  • Interpreter / virtual environment mismatch (pip vs. 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 bare pip install pillow installs into a different environment than python script.py.
    • Always run python -m pip install pillow (or select the matching .venv interpreter in VS Code / Jupyter) so pip targets the exact same environment as python.
  • Conflicting broken PIL install or file shadowing:
    • If an old PIL directory exists alongside pillow in site-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.py or pillow.py in the working directory, which shadows the installed library.
  • Conda / Mamba environments:
    • If you use Anaconda or Miniconda, install via conda install -c conda-forge pillow (or mamba install pillow) inside your activated Conda environment so its compiled C libraries (libjpeg, zlib, libtiff) match your Conda runtime.

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.