Python PIL has no attribute 'Image'

Solution 1:

PIL's __init__.py is just an empty stub as is common. It won't magically import anything by itself.

When you do from PIL import Image it looks in the PIL package and finds the file Image.py and imports that. When you do PIL.Image you are actually doing an attribute lookup on the PIL module (which is just an empty stub unless you explicitly import stuff).

In fact, importing a module usually doesn't import submodules. os.path is a famous exception, since the os module is magic.

More info:
The Image Module

Solution 2:

If you, like me, found the accepted answer a bit befuddling because you could swear you've been able to use

import PIL
PIL.Image

sometimes before, a potential reason for this is if any other code in your Python session has run from PIL import Image or import PIL.Image, even if it's in a completely different scope, you will be able to access PIL.Image.

In particular, matplotlib does so when it's imported. So if you run

import matplotlib
import PIL
PIL.Image

it works. Thanks, Python.

Don't trust anyone. Don't trust Python. Use import PIL.Image.

Solution 3:

You can do:

try:
    import Image
except ImportError:
    from PIL import Image

it's better to use pillow instead PIL.