Modulenotfounderror: no module named ‘pil’

What is Python “modulenotfounderror: no module named ‘pil’?”

The “ModuleNotFoundError: No module named ‘PIL'” error in Python occurs when you forget to install the “pillow” module before importing it.

Or it happens when you install it in the wrong Python environment.

In simple words, this error occurs when you try to import the PIL module in Python, but Python cannot find it.

PIL is an acronym for Python Imaging Library and has been deprecated and substituted by the Pillow library.

Here’s another reason why the error ‘no module named pil’ occurs:

  • You’re trying to use the module in a Python environment where it isn’t installed.
  • Incorrect module name.
  • When you have multiple versions of Python installed, you might be using a different version of the Pillow module.
  • You are running the incorrect version of Python.

What is “pil” module?

The Python Image Library, also known as PIL, was a module designed for Python to handle image processing.

It offered tools for creating, modifying, and exporting image files.

However, in 2011, the PIL project was deprecated.

To keep up with the latest Python versions, the Pillow module was created as a fork from the PIL module.

Over time, Pillow took over from PIL and is now the preferred image-processing library for Python.

How to fix modulenotfounderror: no module named ‘pil’ in Python?

Here are some of the solutions you can take to fix modulenotfounderror: no module named ‘pil’ in Python:

Solution 1: Check the “pil” module if it is installed

To check if pil module is installed you can use the following command:

pip show pil

If the out is like this:

WARNING: Package(s) not found: pi

It simply means that you haven’t installed it; proceed to solution 2.

Solution 2: Install the “pillow” module

If you have the PIL module installed, after checking it, you should uninstall PIL before you install the Pillow module.

pip uninstall PIL

To install the pillow module, execute the following command:

pip install Pillow

Use the following command for Python 3:

pip3 install Pillow

Use the following if you are using Anaconda:

conda install -c conda-forge pillow

Use the following if you are using for Jupyter Notebook:

!pip install Pillow

use the following command if you get a permissions error

sudo pip3 install Pillow
pip install Pillow --user

If you’re using a virtual environment, ensure to activate it before installing Pillow.

Solution 3: Check if the module is installed

To check if you have installed the module successfully use the the following command:

pip show Pillow

Solution 4: Check the Python version

If you have multiple Python versions installed, ensure you’re installing Pillow for the correct version.

To check the Python version, use the following command:

python -V

Or

python --version

Solution 5: Check the module spelling

There are some instances where you’ve misspelled the module, and that’s why you’re encountering this error

Solution 6: Upgrade the version of the Pillow module

You can also try to upgrade the version of the Pillow module.

To do that use the following command:

pip install Pillow --upgrade

Solution 7: Try to reinstall the module

If none of these solutions above doesn’t, you might need to uninstall and reinstall Pillow.

To uninstall, use the following command:

pip uninstall PIL

To reinstall the module use the following command:

pip install Pillow

or

pip3 install Pillow

Remember, it’s always a good idea to use a virtual environment when working with Python to avoid conflicts between different projects and their dependencies.

Conclusion

TheModuleNotFoundError: No module named ‘PIL'” error in Python occurs when you forget to install the “pillow” module before importing it.

To resolve this error, you have to install the module using the “pip”.

By following the guide above, there’s no doubt that you’ll be able to resolve this error quickly.

We hope that you’ve resolved the error with the help of this guide. Thank you for reading, and have fun coding!

Leave a Comment