2012-08-11 12 views
9

Ich benutze Python2.6 und habe heute Morgen ein Problem. Es besagt, dass "Modul" kein Attribut "Image" hat. Hier ist meine Eingabe. Warum kann ich PIL.Image zum ersten Mal nicht benutzen?Python PIL hat kein Attribut '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'> 

Antwort

14

PIL __init__.py ist nur ein leerer Stub wie üblich. Es wird nichts magisch etwas selbst importieren.

Wenn Sie from PIL import Image tun, sieht es im PIL-Paket und findet die Datei Image.py und importiert diese. Wenn Sie PIL.Image tun, tun Sie tatsächlich eine Attributsuche auf dem PIL-Modul (das ist nur ein leerer Stub, wenn Sie nicht explizit Zeug importieren).

In der Tat importiert ein Modul in der Regel nicht importieren Submodule. os.path ist eine berühmte Ausnahme, da das OS-Modul magisch ist.

Weitere Informationen:
The Image Module

-1

Statt from PIL import Image oder import PIL
versuchen

`PIL.Image` 
2

Sie tun können:

try: 
    import Image 
except ImportError: 
    from PIL import Image 

es besser ist, Kissen zu verwenden, anstatt PIL.

Verwandte Themen