others-How to solve ERROR: Could not find a version that satisfies the requirement PIL (from versions: none) or ERROR: No matching distribution found for PIL when trying to execute python script with PIL included.

1. Purpose

In this post, I would demo how to solve the following error when executing a python script with PIL included:

➜  myscripts git:(master) python3 watermark_batch.py image-20211015170843416.png
Traceback (most recent call last):
  File "/Users/bswen/work/python/myutils/myscripts/watermark_batch.py", line 1, in <module>
    from PIL import Image
ModuleNotFoundError: No module named 'PIL'
➜  myscripts git:(master) python3 --version                                     
Python 3.9.7
➜  myscripts git:(master) pip3 install PIL 
DEPRECATION: Configuring installation scheme with distutils config files is deprecated and will no longer work in the near future. If you are using a Homebrew or Linuxbrew Python, please see discussion at https://github.com/Homebrew/homebrew-core/issues/76621
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
ERROR: Could not find a version that satisfies the requirement PIL (from versions: none)
ERROR: No matching distribution found for PIL
WARNING: You are using pip version 21.2.4; however, version 21.3.1 is available.
You should consider upgrading via the '/usr/local/opt/[email protected]/bin/python3.9 -m pip install --upgrade pip' command.
➜  myscripts git:(master) 

The core error message is:

ERROR: Could not find a version that satisfies the requirement PIL (from versions: none)
ERROR: No matching distribution found for PIL

2. The environment

This is the python environment in my laptop:

image-20211030214256234

You can see that I am using python 3.7

3. The solution

The reason for this problem is:

  • Pillow has now been used instead of PIL
  • The most python version supported by PIL is 2.7,
  • And pillow version greater than 2.1 supports python2.6, 2.7 and 3.x,
  • The corresponding original import image is also replaced by from PIL import Image, pay attention to case.

So the solution is installing pillow instead of intalling PIL:

➜  myscripts git:(master) pip3 install pillow  
DEPRECATION: Configuring installation scheme with distutils config files is deprecated and will no longer work in the near future. If you are using a Homebrew or Linuxbrew Python, please see discussion at https://github.com/Homebrew/homebrew-core/issues/76621
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting pillow
  Downloading http://mirrors.aliyun.com/pypi/packages/32/25/32889d2b7c577b5a454dd12194b2ecb9d87d4b49c15c278dc99bc8ff5d2e/Pillow-8.4.0-cp39-cp39-macosx_10_10_x86_64.whl (3.0 MB)
     |████████████████████████████████| 3.0 MB 8.2 MB/s 
Installing collected packages: pillow
DEPRECATION: Configuring installation scheme with distutils config files is deprecated and will no longer work in the near future. If you are using a Homebrew or Linuxbrew Python, please see discussion at https://github.com/Homebrew/homebrew-core/issues/76621
Successfully installed pillow-8.4.0
WARNING: You are using pip version 21.2.4; however, version 21.3.1 is available.
You should consider upgrading via the '/usr/local/opt/[email protected]/bin/python3.9 -m pip install --upgrade pip' command.
➜  myscripts git:(master) python3 watermark_batch.py image-20211015170843416.png
start watermarking file image-20211030210011891.png
start watermarking file image-20211015181246892.png
start watermarking file image-20211015170843416.png
➜  myscripts git:(master) 

4. Summary

In this post, I demonstrated how to solve the ERROR: Could not find a version that satisfies the requirement PIL (from versions: none) or ERROR: No matching distribution found for PIL when trying to execute python script with PIL included. The key point is the ‘PIL’ library is replaced by ‘pillow’ library, we should use the latter to process images.