ImportError No module named PIL
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
ImportError: No module named PIL usually means the Python environment running your code does not have Pillow installed or is not the environment you think it is. The old PIL project is no longer the package you normally install. In modern Python code, the correct library is Pillow, even though imports still use the PIL namespace.
Install Pillow, Not the Original PIL Package
Most projects should fix the error by installing Pillow into the same interpreter that runs the script.
Or, if the project is explicitly using Python 3:
After installation, the import stays the same:
This works because Pillow provides the PIL module path for compatibility.
Verify Which Python Is Running
A very common problem is installing Pillow into one Python environment and running the script with another. Check the interpreter and package location explicitly.
If the interpreter path and the package location do not line up with the environment you expect, the import error is not really about PIL. It is about environment mismatch.
This happens frequently with:
- virtual environments
- system Python vs Homebrew or pyenv Python
- IDEs that use a different interpreter than the terminal
- notebook kernels pointing at another environment
Use Virtual Environments Deliberately
A clean virtual environment avoids many import problems.
If that final command succeeds, the package is installed correctly in that environment.
This is often faster than trying to repair a messy global Python installation.
Jupyter and IDE Cases
If the code runs in Jupyter, VS Code, or PyCharm, the editor or notebook may be attached to a different interpreter than the shell where you installed Pillow.
A quick notebook check:
If the kernel is not using the environment where Pillow was installed, switch the kernel or install Pillow into the active kernel environment.
The same principle applies to IDE run configurations. The import error often persists because the IDE is launching a different interpreter than the developer expects.
Do Not Rename Your Own File to PIL.py
Local naming conflicts can also cause import issues. If your project contains a file named PIL.py or a directory named PIL, Python may import that local path instead of the real Pillow package.
A small sanity check:
If you find a conflicting local name, rename it and try again.
Confirm the Import with a Minimal Script
Reduce the problem to the smallest possible test:
Run it with the same interpreter your project uses. If this fails, the environment is still wrong. If it succeeds, the original project may have a separate path or packaging problem.
Common Pitfalls
The most common mistake is trying to install a package named PIL instead of Pillow. Another is installing Pillow with one interpreter and running the code with another, especially in virtual environments, notebooks, or IDEs. Developers also sometimes fix the package installation but still keep a local file or folder named PIL, which shadows the real library. A final issue is using pip directly instead of python -m pip, which can silently target a different Python installation than the one executing the code.
Summary
- Modern projects should install Pillow, not the old PIL package.
- The import remains
from PIL import Image. - Use
python -m pip install Pillowso installation targets the correct interpreter. - Verify the active interpreter in terminals, IDEs, and notebooks.
- Check for local files or folders that shadow the
PILpackage name.

