Last modified: Aug 11, 2026
[Solved] No module named 'PIL' Error
Encountering a ModuleNotFoundError can be frustrating, especially when you are ready to run your Python script. This specific error, ModuleNotFoundError: No module named 'PIL', is very common among beginners and even experienced developers. It simply means that Python cannot find the PIL library in your current environment.
The good news is that this error is easy to solve. In this guide, we will explain what PIL is, why this error happens, and provide step-by-step solutions to fix it permanently. We will also cover common pitfalls and how to avoid them in the future.
Understanding PIL and Pillow
First, you need to understand the relationship between PIL and Pillow. PIL stands for Python Imaging Library. It was the original library for opening, manipulating, and saving many different image file formats in Python. However, the original PIL project was discontinued in 2011 and does not support Python 3.
Pillow is a modern, actively maintained fork of PIL. It is fully compatible with Python 3 and is the recommended way to use the PIL functionality. When you install Pillow, it provides the same PIL package name. Therefore, when you write import PIL or from PIL import Image, you are actually using Pillow.
The error occurs because the Pillow library is not installed in your Python environment. This can happen if you are using a virtual environment, a different Python interpreter, or if the installation failed previously.
Why You See This Error
There are a few common reasons why you might see this error. The most obvious one is that Pillow is not installed. Another reason could be that you are running your script in a different environment than the one where Pillow is installed. For example, you might have installed Pillow globally but are running your script inside a virtual environment that does not have it.
Sometimes, the issue is with the Python interpreter itself. If you have multiple versions of Python installed, the package might be installed for Python 3.9 but you are running your script with Python 3.10. Always ensure you are using the correct interpreter.
Let's look at the most effective solutions to resolve this issue quickly.
Solution 1: Install Pillow Using pip
The most straightforward solution is to install the Pillow library using pip. Open your terminal or command prompt and run the following command:
pip install Pillow
This command will download and install the latest version of Pillow from the Python Package Index (PyPI). After the installation is complete, try importing PIL again. The error should be resolved.
If you are using Python 3, you might need to use pip3 instead of pip. The command would be:
pip3 install Pillow
If you are on a system where you have both Python 2 and Python 3, using pip3 ensures you are installing the package for Python 3.
Solution 2: Use pip with the -m Flag
Sometimes, the pip command might point to a different Python installation. To be absolutely sure you are installing the package for the Python interpreter you are using, use the python -m pip command. This is a safer method and is recommended for all users.
Run the following command in your terminal:
python -m pip install Pillow
Alternatively, if your Python command is python3, use:
python3 -m pip install Pillow
This method ensures that pip is invoked from the same Python interpreter that you are using to run your scripts. This eliminates the risk of installing to the wrong environment.
Solution 3: Install in a Virtual Environment
If you are using a virtual environment, you need to activate it first before installing the package. A virtual environment is a self-contained directory that contains a Python installation and a set of libraries. This is a best practice for project isolation.
First, activate your virtual environment. On Windows, use:
venv\Scripts\activate
On macOS and Linux, use:
source venv/bin/activate
Once the virtual environment is activated, you will see its name in your terminal prompt. Now, install Pillow:
pip install Pillow
Now, run your script. It should work without any import errors. Remember to always activate your virtual environment before installing any packages or running your code.
Solution 4: Upgrade pip and Setuptools
In some cases, an outdated pip or setuptools can cause installation issues. It is a good idea to upgrade these tools before installing new packages. Run the following commands:
python -m pip install --upgrade pip
python -m pip install --upgrade setuptools
After upgrading, try installing Pillow again. This can resolve hidden dependency conflicts or installation failures that were not obvious.
Solution 5: Check Your Python Environment
If you have tried all the above solutions and still see the error, there might be a problem with your Python environment. Use the following command to see which Python interpreter you are using:
which python
On Windows, use where python. This will show you the path to your Python executable. Then, check if Pillow is installed for that specific interpreter by running:
python -c "import PIL; print(PIL.__version__)"
If you get a ModuleNotFoundError, then Pillow is indeed not installed for that interpreter. If you get a version number, then the library is installed, and the issue might be with your IDE or code editor using a different interpreter.
Example Code and Output
Let's look at a simple example that uses PIL to open and display an image. This code will help you verify that the installation was successful.
# Import the necessary module
from PIL import Image
# Open an image file
try:
img = Image.open("example.jpg")
print("Image opened successfully!")
print("Image size:", img.size)
except FileNotFoundError:
print("Error: The image file was not found.")
except Exception as e:
print("An error occurred:", e)
If you have Pillow installed and you have an image named example.jpg in the same directory, the output will be:
Image opened successfully!
Image size: (1920, 1080)
If the image is not found, you will see a clear error message. This confirms that the import statement works correctly and the issue is with your file path, not the library.
Common Mistakes to Avoid
One common mistake is trying to install the original PIL package. Avoid running pip install PIL because this will fail. Always use pip install Pillow.
Another mistake is running your script from an IDE like VS Code or PyCharm without setting the correct Python interpreter. Make sure your IDE is using the same interpreter where you installed Pillow. You can usually change this in the IDE's settings.
Finally, do not ignore the output of the pip install command. If there is an error during installation, read the message carefully. It often contains the solution to the problem.
Conclusion
Fixing ModuleNotFoundError: No module named 'PIL' is a simple process once you understand the root cause. The solution is to install the Pillow library, which provides the PIL package. Always use pip install Pillow and ensure you are installing it in the correct Python environment.
We have covered multiple solutions, from basic installation to more advanced environment checks. By following these steps, you can resolve this error and continue working with images in Python. Remember to use virtual environments for your projects and always check your Python interpreter settings. Happy coding!