Python PIL has no attribute 'Image'

Viewed 63176

I'm using python2.6 and got a problem this morning. It said 'module' has no attribute 'Image'. Here is my input. Why the first time I can not use PIL.Image?

>>> import PIL
>>> PIL.Image
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Image'
>>> from PIL import Image
>>> Image
<module 'PIL.Image' from '/usr/lib/python2.6/dist-packages/PIL/Image.pyc'>
>>> PIL.Image
<module 'PIL.Image' from '/usr/lib/python2.6/dist-packages/PIL/Image.pyc'>
4 Answers

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.

The solution is

instead of using this form

import PIL
PIL.Image

use this form

from PIL import Image
Image

in the Linux system both forms work fine

enter image description here

but in windows, there is a problem

enter image description here

Related