Could not import PILLOW_VERSION from PIL

Question:

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.

Asked By: kramer

||

Answers:

Pillow 7.0.0 removed PILLOW_VERSION, you should use __version__ in your own code instead.


Edit (2020-01-16):

If using torchvision, this has been fixed in v0.5.0. To fix:

  1. Require torchvision>=0.5.0
  2. If Pillow was temporarily pinned, remove the pin

Old info (2020-01-09):

If using torchvision, there is a release planned this week (week 2, 2020) to fix it:

The options are:

  • wait for the new torchvision release
  • use the master version of torchvision (eg. pip install -U git+https://github.com/pytorch/vision)
  • install torchvision from a nightly, which also requires a pytorch from a nightly version
  • or install Pillow<7 (eg. pip install "pillow<7")
Answered By: Hugo

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:UsersUserNameAppDataLocalProgramsPythonPython37Libsite-packagestorchvisiontransformsfunctional.py

  • __init__.py:C:UsersUserNameAppDataLocalProgramsPythonPython37Libsite-packagesPIL__init__.py

Answered By: PrasadHeeramani

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"
Answered By: sam
  • 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

Answered By: Maddu Swaroop

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.

Answered By: Mikhail_Sam

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
Answered By: Jitendra Dhiman

The less painful way that worked for me is using alias

from PIL import __version__ as PILLOW_VERSION

Why?

Pillow is forked from PIL 1.1.7
VERSION was removed in Pillow 6.0.0
PILLOW_VERSION was removed in Pillow 9.0.0
Use __version__ instead

When you use some older packages, they try to import PILLOW_VERSION which is no longer available. So you’ll need to use alias.

Answered By: Long