Pillow: "import Image" is different than "from PIL import Image"

Created on 11 Nov 2014  Ā·  5Comments  Ā·  Source: python-pillow/Pillow

I noticed that when I import Image directly (python 2.7.8, Open SuSE Linux 13.2, 64bits, Pillow 2.5.1)
Pillow does not work. For example the following code:

    import Image
    im = Image.open("git01.jpeg")
    print im__

gives the following error message:

Traceback (most recent call last):
File "test2.py", line 2, in
im = Image.open("git01.jpeg")
File "/usr/lib64/python2.7/site-packages/PIL/Image.py", line 2256, in open
% (filename if filename else fp))
IOError: cannot identify image file 'git01.jpeg'

But if Image is imported from PIL everything is fine. The following code:

    from PIL import Image
    im = Image.open("git01.jpeg")
    print im

gives the correct answer:

This happens because python, somehow, loads two (2) independent copies of the Image module simultaneously. For example the following code:

    import Image
    print id(Image)
    import PIL.Image
    print id(PIL.Image)
    from PIL import Image
    print id(Image)__

gives two different ids:

140590133857984
140590088254472
140590088254472

As a solution, I suggest that the capability to import the Image module directly
be removed (i.e remove the PIL.pth file from the installation).

Thanasis

Most helpful comment

It is also written in the PIL manual.

But this is not a PIL, this is Pillow, and in Pillow written:

Pillow >= 1.0 no longer supports ā€œimport Imageā€. Please use ā€œfrom PIL import Imageā€ instead.

All 5 comments

I suggest that the capability to import the Image moduled directly
be removed

But Image can't be imported without PIL. Probably this is old version of Pillow or PIL in your environment.

In [1]: import Image
ImportError: No module named Image
In [2]: from PIL import Image

I have tested that import Image works in various Linux distributions (SuSE, PCLINUXOS, UBUNTU) and other OSes (FreeBSD, OpenBSD, Windows) for more than 10 years (with PIL). You can import Image in OpenSuSE 13.2 which was out just last week (with Pillow). It is also written in the PIL manual.

Probably this is PILcompat module provided by distributive maintainers.

>>> Image.__file__
'/usr/lib/python2.7/dist-packages/PILcompat/Image.pyc'
>>> PIL.__file__
'/usr/lib/python2.7/dist-packages/PIL/__init__.pyc'

As this is not a part of Pillow, this is not a Pillow issue.

It is also written in the PIL manual.

But this is not a PIL, this is Pillow, and in Pillow written:

Pillow >= 1.0 no longer supports ā€œimport Imageā€. Please use ā€œfrom PIL import Imageā€ instead.

On OS X I get:

>>> import Image
>>> print id(Image)
4443078848
>>> import PIL.Image
>>> print id(PIL.Image)
4445611016
>>> from PIL import Image
>>> print id(Image)
4445611016

>>> Image.__file__
'/usr/local/lib/python2.7/site-packages/PIL/Image.pyc'
>>> PIL.__file__
'/usr/local/lib/python2.7/site-packages/PIL/__init__.pyc'
Was this page helpful?
0 / 5 - 0 ratings

Related issues

boskicthebrain picture boskicthebrain  Ā·  4Comments

thinrhino picture thinrhino  Ā·  3Comments

maxhumber picture maxhumber  Ā·  3Comments

SysoevDV picture SysoevDV  Ā·  3Comments

HansHirse picture HansHirse  Ā·  3Comments