No module named 'PIL.image'

Hey all

I’m currently getting this error No module named 'PIL.image' when trying to import from fastai.vision.all import *. For context, this is in a Google Colab environment and the only code I’ve run before this line is the standard !pip install -Uqq fastai.

I’m not sure if this is maybe a default version change by Colab for PIL, as the notebook was working fine a few months ago when I last ran it. Does anyone have any idea as to what might be causing this?

Many thanks

I am getting the same error in colab. One thing I noticed is that the colab Python version is 3.7.13 and their fastai version is 2.7.2 When I try it locally, it is a few minor versions of the library back (fastai 2.6.3) It still seems to work in my local install. My python version is 3.7.11

Can you please provide the pillow version installed?

I got a different error than what’s been reported here, and I had to upgrade my pillow version

On my local install where I don’t get the error:

PIL.__version__
'8.4.0'

On Colab where I get the error:

import PIL
PIL.__version__
7.1.2

And what’s the full error log?

I think yours is different than @BHouwens, given his is a "No module named PIL.image". This PR should fix yours: Fix AttributeError with PIL by muellerzr · Pull Request #3702 · fastai/fastai · GitHub

On Colab, just copied @BHouwens steps (out of sheer curiosity as it was working for me a month ago) . Here is my log:

!pip install -U fastai
from fastai.vision.all import *

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-9-32a786866fa9> in <module>()
----> 1 from fastai.vision.all import *
      2 

3 frames
/usr/local/lib/python3.7/dist-packages/fastai/vision/all.py in <module>()
      2 from ..basics import *
      3 from ..callback.all import *
----> 4 from .augment import *
      5 from .core import *
      6 from .data import *

/usr/local/lib/python3.7/dist-packages/fastai/vision/augment.py in <module>()
     16 #nbdev_comment from __future__ import annotations
     17 from ..data.all import *
---> 18 from .core import *
     19 from .data import *
     20 

/usr/local/lib/python3.7/dist-packages/fastai/vision/core.py in <module>()
     16 
     17 from PIL import Image
---> 18 try: BILINEAR,NEAREST = Image.Resampling.BILINEAR,Image.Resampling.NEAREST
     19 except ModuleNotFoundError: from PIL.Image import BILINEAR,NEAREST
     20 

/usr/local/lib/python3.7/dist-packages/PIL/Image.py in __getattr__(name)
     59             _raise_version_warning()
     60             return __version__
---> 61         raise AttributeError("module '{}' has no attribute '{}'".format(__name__, name))
     62 
     63 

AttributeError: module 'PIL.Image' has no attribute 'Resampling'

Okay tried this with a manual install of Pillow to 9.0.0 and it seems to remove this issue. So I had to add a cell to the notebook for

!pip install Pillow==9.0.0

Okay @mike.moloch @muellerzr I’ve figured out what the problem is. The Pillow package removed the Resampling enum (as well as other enums) at some point, I’m guessing possibly around 7.1.2. These enums were reintroduced in an MR for 9.1.0 (you can view the pull request here).

So the solution in Colab is to preface the fastai vision import with:

!pip install Pillow=9.1.0

This will require a runtime restart, which is quite annoying, but if you run PIL.__version__ and it’s on 9.1.0 the imports should work. Maybe just confirm on your side that I’m correct, then I can mark this as the solution. Thanks to you both!

1 Like

Cool, then this PR still fixes the issue just fine then. Perfect.

1 Like