vision: New Pillow version (7.0.0) breaks torchvision (ImportError: cannot import name 'PILLOW_VERSION' from 'PIL')

Hi, it looks like Pillow released version 7.0.0 so I can no longer import torchvision without getting this error: ImportError: cannot import name 'PILLOW_VERSION' from 'PIL'

Pinning to Pillow 6.2.1 fixes the issue.

I see that you fixed this for a future torchvision release (https://github.com/pytorch/vision/pull/1501). Do you know when this will be released? If it will be awhile, could the version of Pillow be pinned to be less than 7.0.0 in the meantime?

Thanks.

Versions: torch: 1.3.1 torchvision: 0.4.2 Pillow: 7.0.0

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 83
  • Comments: 62 (15 by maintainers)

Commits related to this issue

Most upvoted comments

Until the new torchvision release is out, the workaround is either to use the master version of torchvision (eg. pip install -U git+https://github.com/pytorch/vision), or Pillow<7 (eg. pip install "pillow<7").

I still need to use pillow<7 workaround with:

torchvision             0.14.0.dev20220830+cpu

Is this supposed to be fixed? I see this issue is closed.

This might be late, but for fellow travelers led to this by Google, a simpler workaround for this, instead of downgrading can be done by patching the torchvision/transforms/functional.py file. If you remove the PILLOW_VERSION import from the import and then replace PILLOW_VERSION with PIL.version in line 727, then it works without a downgrade. Official PIL docs recommends using version instead.

Hi,

Thanks for the report!

We will be releasing a new version of PyTorch and torchvision early next week, so this should be soon fixed.

As such, I’m closing the issue, but let me know if you have further questions.

Yes, torchvision v0.5.0 has been released with the fix:

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

pytorch 1.4.0, pillow 7.0.0, torchvision 0.5.0 works ok for me, so instead of pillow downgrading to 6.x you could update torchvision:

# use
pip install 'torchvision>=0.5.0'
# or
conda install -c pytorch 'torchvision>=0.5.0'

Yes, torchvision v0.5.0 has been released with the fix:

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

Is there a workaround until then? Is there a version that does not depend on pillow 7.0.0?

I downgraded pillow version to 6.2.1 which seems to work without any issues. If you’re using anaconda distrib, then use the below command for downgrading pillow version:

$ conda install -c conda-forge pillow==6.2.1

Hi @pennarellor and @saipreethamsata

I had the same error after resolving the Pillow version issue. Restarting the Jupyter notebook kernel resolved it.

seems this temp soln works:

conda install pillow=6.2.1 -y

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

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

This was enough!, thanks

The fact that Torchvision accepts incompatible Pillow versions is a bug in Torchvision requirements, isn’t it?

Hi,

Firstly, thanks a lot for some great advice above to resolve the PILLOW_VERSION problem. However, I’m still unable to resolve the issue with the torchvision==0.5.0, because I can’t seem to install torchvision version 0.5.0.

I tried to install torchvision==0.5.0 with conda. But everytime the version installed is torchvision==0.3.0. I tried to uninstall and conda install -c ‘torchvision>=0.5.0’ multiple times but doesn’t seem to install torchvision 0.5.0. Is it because my cuda toolkit version is 9.0? Can somebody please help? I spent hours trying to get this working without having to remove the PILLOW_VERSION from the functional.py as mentioned above in one solution. Thanks much for any suggestions and tips. I’d really appreciate!!

Hi @fmassa Thanks so much for getting back. I suspected that it must be CUDA 9.0. I didn’t update the toolkit and used the workaround mentioned before to remove the PILLOW_VERSION. It seemed to work. But you are right, I should update the CUDA toolkit and then install torchvision 0.5.0. Thanks again for your help!!

You can also manually remove PILLOW_VERSION from python3.7/site-packages/torchvision/transforms/functional.py ` from PIL import Image, ImageOps, ImageEnhance, PILLOW_VERSION

`

This worked for me. Was having the issue with pillow==6.1, torchvision==0.4.2.

@digantamisra98 expected release date is tonight

If you want to use pillow of latest version, you do following

open following file. C:\User{admin}\Anaconda3\envs\pytorch\Lib\site-packages\torchvision\transforms\functional.py After do comment , you should change from PILLOW_VERSION to __version__ #from PIL import Image, ImageOps, ImageEnhance, PILLOW_VERSION from PIL import Image, ImageOps, ImageEnhance, __version__

Error caused: #VERSION was removed in Pillow 6.0.0. #PILLOW_VERSION was removed in Pillow 7.0.0. #Use version instead. __version__ = _version.version

Hi all, I upgraded to torchvision 0.5.0 and the pillow 7.0.0 issue is solved, but now I get this error

ImportError: cannot import name '_HAS_OPS' from 'torchvision.extension'

torch: 1.4.0 torchvision: 0.5.0 pillow: 7.0.0

we dont distributed torchvision-cpu packges, but we do distributed Conda CPU-only torchvision packages. They are installable via a label: conda install pytorch torchvision cpuonly -c pytorch This is documented on pytorch.org , in the install selector, just select CUDA=None

Thank you @fmassa and @hugovk, its working like a charm now!

As @hugovk mentioned, you will need to update torchvision to the latest stable release to fix this.

Hello, Was this issue fixed? I am still getting an error here while importing torchvision. I am using version 0.4.2 (build channel: cuda100py37hecfc37a_0) and getting an Import error.

PIL version is 7.0.0 and build channel is py37hb39fc2d_0

Here are the pytorch versions and build channels _pytorch_select is 0.2 with build channel gpu_0
pytorch version is 1.3.1 with build channel cuda100py37h53c1284_0

Pasting error log below for reference

ImportError Traceback (most recent call last) <ipython-input-1-6842f2896ae5> in <module> 9 import torch.nn.functional as F 10 from torch.utils.data import DataLoader, Dataset, TensorDataset —> 11 from torchvision import transforms, utils 12 from skimage import io, transform 13

~/anaconda3/envs/fastai/lib/python3.7/site-packages/torchvision/init.py in <module> 2 3 from torchvision import models ----> 4 from torchvision import datasets 5 from torchvision import ops 6 from torchvision import transforms

~/anaconda3/envs/fastai/lib/python3.7/site-packages/torchvision/datasets/init.py in <module> 7 from .svhn import SVHN 8 from .phototour import PhotoTour ----> 9 from .fakedata import FakeData 10 from .semeion import SEMEION 11 from .omniglot import Omniglot

~/anaconda3/envs/fastai/lib/python3.7/site-packages/torchvision/datasets/fakedata.py in <module> 1 import torch 2 from .vision import VisionDataset ----> 3 from … import transforms 4 5

~/anaconda3/envs/fastai/lib/python3.7/site-packages/torchvision/transforms/init.py in <module> ----> 1 from .transforms import *

~/anaconda3/envs/fastai/lib/python3.7/site-packages/torchvision/transforms/transforms.py in <module> 15 import warnings 16 —> 17 from . import functional as F 18 19 if sys.version_info < (3, 3):

~/anaconda3/envs/fastai/lib/python3.7/site-packages/torchvision/transforms/functional.py in <module> 3 import sys 4 import math ----> 5 from PIL import Image, ImageOps, ImageEnhance, PILLOW_VERSION 6 try: 7 import accimage

ImportError: cannot import name ‘PILLOW_VERSION’ from ‘PIL’ (/home/aash/anaconda3/envs/fastai/lib/python3.7/site-packages/PIL/init.py)

@SaschaHornauer we follow the CUDA support from PyTorch, which is the last minor version of the two last major versions IIRC

Hi @skudekar

We don’t provide binaries anymore for CUDA 9.0, only for CUDA 9.2 and CUDA 10.1. That’s the reason why you are not being able to update torchvision.

Can you try installing a newer version of the CUDA toolkit?

When will the new TorchVision version containing the fix for Pillow dependency be out. My build via CircleCI is failing due to the same? Can I force solve this for the time being?

You can also manually remove PILLOW_VERSION from python3.7/site-packages/torchvision/transforms/functional.py ` from PIL import Image, ImageOps, ImageEnhance, PILLOW_VERSION

`

Is there a workaround until then? Is there a version that does not depend on pillow 7.0.0?

I downgraded pillow version to 6.2.1 which seems to work without any issues. If you’re using anaconda distrib, then use the below command for downgrading pillow version:

$ conda install -c conda-forge pillow==6.2.1

Rather use 6.2.2, which is a security update:

$ conda install -c conda-forge pillow==6.2.2

Or:

$ conda install -c conda-forge pillow"<7.0.0"