A common error you may encounter when using Python is modulenotfounderror: no module named ‘pil’.
This error occurs when the Python interpreter cannot detect the Pillow library in your current environment.
Pillow is built on top of PIL (Python Image Library).
PIL is no longer supported, and you should always use Pillow.
This tutorial goes through the exact steps to troubleshoot this error for the Windows, Mac and Linux operating systems.
Table of contents
ModuleNotFoundError: no module named ‘pil’
What is PIL?
PIL stands for Python Imaging Library and is a library for image processing. We often use PIL together with other image processing libraries like OpenCV.
What is the Difference Between PIL and Pillow?
Pillow is a fork of the PIL library; in other words, it is built on top of PIL. PIL support ended in 2011; therefore, it is no longer up-to-date nor secure to use. If you want to use the image processing capabilities that PIL provided, you should use the Pillow library.
The simplest way to install Pillow is to use the package manager for Python called pip. The following installation instructions are for the major Python version 3.
How to Install Pillow on Windows Operating System
First, you need to download and install Python on your PC. Ensure you select the install launcher for all users and Add Python to PATH checkboxes. The latter ensures the interpreter is in the execution path. Pip is automatically on Windows for Python versions 2.7.9+ and 3.4+.
You can check your Python version with the following command:
python3 --version
You can install pip on Windows by downloading the installation package, opening the command line and launching the installer. You can install pip via the CMD prompt by running the following command.
python get-pip.py
You may need to run the command prompt as administrator. Check whether the installation has been successful by typing.
pip --version
Pillow installation on Windows Using pip
To install Pillow, run the following command from the command prompt.
pip3 install pillow
How to Install Pillow on Mac Operating System using pip
Open a terminal by pressing command (⌘) + Space Bar to open the Spotlight search. Type in terminal and press enter. To get pip, first ensure you have installed Python3:
python3 --version
Python 3.8.8
Download pip by running the following curl command:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
The curl command allows you to specify a direct download link. Using the -o option sets the name of the downloaded file.
Install pip by running:
python3 get-pip.py
From the terminal, use pip3 to install Pillow:
pip3 install pillow
How to Install Pillow on Linux Operating Systems
All major Linux distributions have Python installed by default. However, you will need to install pip. You can install pip from the terminal, but the installation instructions depend on the Linux distribution you are using. You will need root privileges to install pip. Open a terminal and use the commands relevant to your Linux distribution to install pip.
Installing pip for Ubuntu, Debian, and Linux Mint
sudo apt install python-pip3
Installing pip for CentOS 8 (and newer), Fedora, and Red Hat
sudo dnf install python-pip3
Installing pip for CentOS 6 and 7, and older versions of Red Hat
sudo yum install epel-release
sudo yum install python-pip3
Installing pip for Arch Linux and Manjaro
sudo pacman -S python-pip
Installing pip for OpenSUSE
sudo zypper python3-pip
Pilow installation on Linux with Pip
Once you have installed pip, you can install Pillow using:
pip3 install pillow
Installing Pillow Using Anaconda
First, to create a conda environment to install PIL.
conda create -n pillow python=3.6
Then activate the pillow container. You will see “pillow” in parentheses next to the command line prompt.
source activate pillow
Now you’re ready to install Pillow using conda.
Anaconda is a distribution of Python and R for scientific computing and data science. You can install Anaconda by going to the installation instructions. Once you have installed Anaconda and created your conda environment, you can install Pillow using one of the following commands:
conda install -c intel pillow
conda install -c intel/label/oneapibeta pillow
Check Pillow Version
Once you have successfully installed Pillow, you can use two methods to check the version of Pillow. First, you can use pip show from your terminal. Remember that the name of the Pillow package is pillow, not PIL.
pip show pillow
Name: Pillow
Version: 8.2.0
Summary: Python Imaging Library (Fork)
Second, within your python program, you can import pillow and then reference the __version__ attribute:
import PIL
print(PIL.__version__)
Note that you need to import PIL, though you installed pillow. Pillow is simply a repackaged, updated version of PIL.
8.2.0
If you used conda to install Pillow, you could check the version using the following command:
conda list -f pillow
# Name Version Build Channel
pillow 8.3.1 py36ha4cf6ea_0
Summary
Congratulations on reading to the end of this tutorial. The modulenotfounderror occurs if you misspell the module name, incorrectly point to the module path or do not have the module installed in your Python environment. If you do not have the module installed in your Python environment, you can use pip to install the package. However, you must ensure you have pip installed on your system. You can also install Anaconda on your system and use the conda install command to install Pillow. Remember to use Pillow, and you have to use the import statement: import PIL
For further reading on installing modules in Python, go to the article: How to Solve Python is ModuleNotFoundError: no module named ‘selenium’.
Go to the online courses page on Python to learn more about Python for data science and machine learning.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.