[SOLVED] Python No Module Named Pil

Python Image Library or PIL is an image processing module developed for Python. It provides image processing tools that help in creating, editing, and exporting image files. However, the PIL project was abandoned in 2011. To continue providing support for the latest Python versions, the Pillow module forked the PIL module. The Pillow module eventually replaced PIL and became the go-to image processing library for Python.

The Python interpreter may not detect specific modules when imported. Therefore, upon execution, Python will return the ModuleNotFoundError: No module named 'pil' exception and exits the program.

For example, let’s say we’re importing a package called “run”. We would do so by using the import statement followed by the package name; run. Since there is no module called “run”, this error is shown.

No module named "run"

Causes & Solutions For No Module Named Pil

There are multiple reasons why this exception may be raised. Let’s look at the following instances.

Package Not Installed Properly

This is probably the most typical reason for this exception. Some libraries may not be part of the Python Standard Library. Therefore they require additional installation steps before we can use them.

In specific IDEs (e.g., spyder), make sure your module are all installed in a single folder. PIP installer may install your package in one place, whereas the IDE expects the module in another directory. Make sure to save all external dependencies in one place.

It is vital to ensure that the correct installation commands for provided to install Pillow.

Windows Installation
python3 -m pip install --upgrade pip
python3 -m pip install --upgrade Pillow

macOS Installation
python3 -m pip install --upgrade pip
python3 -m pip install --upgrade Pillow

Linux Installation
python3 -m pip install --upgrade pip
python3 -m pip install --upgrade Pillow

Multiple Python Versions Installed

Installing multiple versions of Python can result in multiple library folders for each version. Therefore, ensure the library is present in the correct version of Python.

Here’s how we can find the installation location for your version of Python

Open up the Python command terminal

Python application 3.10

Type the following lines of commands

import os
import sys
os.path.dirname(sys.executable)

Output

Python location

So the location would be: ‘C:\Users\Admin\AppData\Local\Programs\Python\Python310’

Incorrect Package Name

Certain packages have import statements that are different from their actual names. One notable example is a web scraping library called BeautifulSoup. The common user may attempt to import the module by calling import BeautifulSoup.

However, this is incorrect. You must go through the documents of each module and figure out the correct import command to avoid confusion. Furthermore, the correct implementation for the said example would be: import bs4

Importing Files

Another reason for the error to arise is importing Python files incorrectly. For example, let’s assume we’re importing the file “program.py”. To import this, we run “import program” excluding the .py extension. It is also essential to ensure that the file is present in the working directory. If failed to do so, the following error will appear:

No module named program

Another rule to remember is that do not use names of pre-existing packages as your Python file name i.e pil.py. If done otherwise, the Python interpreter will consider pandas as your Python file rather than the pil package itself. This will cause problems when working with your file and the package.

Python No Module Named Pil Visual Studio Code

Users tend to face an issue with the module in VSCode. Upon importing, the program may run perfectly, but under the problems tab, this may appear:

import PIL could not be resolved from source. Pylance(reportmissingmodulesource) 

This error is caused due to the fact that VS Code isn’t running the same Python interpreter between the two or more versions. Modules are installed on one of the Python versions you have. Therefore, we must determine the path to the Python executable. Refer to the following command

import sys
print(sys.executable)

This will show us the location in which Python is installed. If you run the same command on VS Code, it will show a different location. In order to have the versions in the same location, we must install the Python interpreter that is being used by VS Code. Type this in your terminal

/path/to/python/used/by/vs/code/python -m pip install pillow

No Module Named pil.imagetk

For Versions Below Python 3

This is due to the fact that Python is installed in a different location than the standard one. This can be fixed by re-installing Python to the standard location or by appending the location to sys.path.

For Versions Above Python 3

Python 3 and above imagetk comes under the Pillow module. Therefore, in order to import, run the following command.

from PIL import ImageTk

No Module Named PIL (macOS)

PIL is a deprecated project, and no further support is provided. However, the Pillow project forked PIL to provide support for the latest Python versions. For macOS machines, the following command needs to be entered.

sudo pip install pillow

In some instances, the following error may appear.

ImportError: No module named PIL

An easy solution to this problem is to make sure PIP is upgraded to the latest versions. PIP makes sure all your packages are unzipped and loaded into Python, ready to use. Therefore, it is vital to upgrade PIP in order to install the latest versions of modules. You can upgrade PIP using the following command in macOS

sudo pip install  --upgrade pip

No Module Named PIL (Ubuntu)

In some versions of PIL in Ubuntu, it may come under the name imaging. Therefore in order to install Pillow in Ubuntu, run the following command.

sudo apt-get install python-imaging

No Module Named PIL (Raspberry Pi)

Upon installing the module on your Pi 0, you may be presented with the following error

sudo apt-get install python-pillow python-pil
Reading package lists... Done
Building dependency tree
Reading state information... Done
Note, selecting 'python-pil' instead of 'python-pillow'
python-pil is already the newest version (5.4.1-2+deb10u2).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

Despite the successful installation, you may be presented with No Module Named Pil error upon running your Python file.

This may be due to the fact that the version of PIL and Python do not match. Make sure your Pi has the correct version of Python installed, and it makes sure the PIL installation matches your Python version.

Pil.exiftags file missing from the Module

Upon installation, the .exiftags function may not work. This may be due to the fact that other versions of the same module may cause the file to be deleted or overwritten.

Deleting all module versions and only saving the latest one can fix this issue.

FAQs

The module name is Pillow, but we use pil to import it. Why?

Support for Python Image Library has been discontinued. Therefore, for the module to work on newer Python versions, the Pillow project forked the existing PIL project. All functionalities from PIL are present in Pillow as well.

How to add pillow in requirements.txt?

Add RUN apk add zlib-dev jpeg-dev gcc musl-dev within your docker file and add Pillow within the requirements.txt file

What is the use of the Pillow module in Python?

Pillow provides image processing capabilities for Python. It has multiple file format support and powerful image processing abilities.

Conclusion

We’ve gone through various causes for Python No Module Named Pil exception. Make sure to keep these reasons in mind, as most “No Module Named” errors have similar reasons.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments