benekastah
benekastah

Reputation: 5711

Why can I import PIL two different ways (and why is it problematic to do it)?

These both kind of work:

import Image

and

from PIL import Image

The import itself never fails, but across the codebase I'm working on both styles have been used. Sometimes one style is preferred over another because one of them didn't work properly. As a specific example, the open method wasn't available from the Image module unless I used the from PIL import Image version.

Here is the result of a recent repl session:

Python 2.6.6 (r266:84292, Apr 12 2013, 18:59:33)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Image
>>> Image
<module 'Image' from '/path/to/python2.6/site-packages/PIL/Image.pyc'>
>>> from PIL import Image as Img
>>> Img
<module 'PIL.Image' from '/path/to/python2.6/site-packages/PIL/Image.pyc'>
>>> Img == Image
False

I have a hunch if I change the codebase to only use one of the styles it will fix the problem, but I'm still not sure why python will import the same file as two separate modules. Nor am I able to understand why these modules seem to interact in bad ways. Any insight?

Upvotes: 1

Views: 90

Answers (2)

Mark Ransom
Mark Ransom

Reputation: 308206

Check your sys.path. I'm betting it includes both the directory that PIL resides in (/path/to/python2.6/site-packages/), plus the subdirectory with PIL as part of it (/path/to/python2.6/site-packages/PIL/). You shouldn't have the one with the subdirectory included.

import sys
print sys.path

Upvotes: 1

Totem
Totem

Reputation: 7369

I was trying to use PIL some time ago, and had major difficulty. My problems were similar to yours I think. I ended up finding a module called Pillow which seemed to be a fork of PIL, which worked for me. Here are some links, and I hope it's of some help to you too.

here are some docs:

http://pillow.readthedocs.org/en/latest/

downloads here:

http://www.lfd.uci.edu/~gohlke/pythonlibs/#pillow

Upvotes: 1

Related Questions