'Could not import PILLOW_VERSION from PIL

While importing, Python (Anaconda) gives the following error:

ImportError: cannot import name 'PILLOW_VERSION' from 'PIL'

I tried removing pillow and then conda install but the error persists.



Solution 1:[1]

Downgrade pillow if you don't need latest features

pip uninstall pillow
pip install "pillow<7"

Or for anaconda,

conda install -c anaconda pillow<7

Solution 2:[2]

I have solved by modifying functional.py and __init__.py which are mentioned in error message.Error.

Modify from PIL import Image, ImageOps, ImageEnhance, PILLOW_VERSION to from PIL import Image, ImageOps, ImageEnhance, __version__ in functional.py approx line number 5.

Modify PILLOW_VERSION = __version__ = _version.__version__ to __version__ = __version__ = _version.__version__ in __init__.py, approx line no 22.

File path:

  • functional.py:C:\Users\UserName\AppData\Local\Programs\Python\Python37\Lib\site-packages\torchvision\transforms\functional.py

  • __init__.py:C:\Users\UserName\AppData\Local\Programs\Python\Python37\Lib\site-packages\PIL\__init__.py

Solution 3:[3]

  • Currently using torchvision==0.4.2 and this solved my problem.
  • Downgrade your pillow to 6.1 and restart your Jupyter notebook.

    Use this

    conda install pillow=6.1

Solution 4:[4]

I found one another good solution: Install Pillow-SIMD instead of Pillow.

Pillow-SIMD is "following" Pillow. Pillow-SIMD versions are 100% compatible drop-in replacements for Pillow of the same version. SIMD stands for "single instruction, multiple data" and its essence is in performing the same operation on multiple data points simultaneously by using multiple processing elements.

Homepage: https://github.com/uploadcare/pillow-simd

Benchmarks: https://python-pillow.org/pillow-perf/

Install instructions:

$ pip uninstall pillow
$ CC="cc -mavx2" pip install -U --force-reinstall pillow-simd

I checked: it has not such error and works on Pytorch/Torchvision.

You don't need to downgrade packages or change source code.

Solution 5:[5]

This is my fix to the error

Error: Could not import PILLOW_VERSION from PIL
OS: Linux 18.0 (LUBUNTU)
Python: 3.6

Resolved the issue by downgrading pillow:

pillow: 7.0.0-py36hb39fc2d_0 --> 6.1.0-py36h34e0f95_0
command: conda install pillow=6.1

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 HaoZeke
Solution 2
Solution 3 Maddu Swaroop
Solution 4 Mikhail_Sam
Solution 5 Suraj Rao