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'
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.-
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
-
Enter the following command to install Pillow:
pip install pillow
Test Environment
- Python 3.12.2 (tags/v3.12.2:6abddd9, Feb 6 2024, 21:26:36) [MSC v.1937 64 bit (AMD64)]
- pip 24.0 (python 3.12)
- Microsoft Windows 11 Enterprise Version 23H2 OS Build 22631.3155 Experience: Windows Feature Experience Pack 1000.22684.1000.0