Python - Error No module named ‘PIL’ occurs

The error ModuleNotFoundError: No module named 'PIL' occurs when the image processing library Pillow is not installed. To resolve this, install Pillow using the pip command.

Example of error occurrence

D:\test> python test.py
Traceback (most recent call last):
  File "D:\test\test.py", line 1, in <module>
    from PIL import Image
ModuleNotFoundError: No module named 'PIL'
ModuleNotFoundError
ModuleNotFoundError

Cause of the error

In Python, a ModuleNotFoundError occurs when the specified module (library) cannot be found. In this case, it is looking for a module named “PIL”, but it cannot find it, so an error occurs. “PIL” is an image processing library called Pillow, and this error occurs because this library is not installed.

Solution

Use the pip command to install Pillow. Pip is a package management tool for Python, which allows you to easily install, update, and uninstall Python libraries.
  1. Launch the command line tool according to the OS you are using.
    • For Windows, use Command Prompt or PowerShell
    • For macOS, use Terminal
    • For Linux and other OS, use the appropriate command line tool
  2. Enter the following command to install Pillow:
    pip install pillow
    
    pip install pillow
    pip install pillow

Test Environment