2

In python, I can type import Image to import the Python Imaging Library (PIL). I can just type that instead of from PIL import Image, like everyone else seems to do. How come I can do this? Is there any difference between from PIL import Image and import Image?

Should I assume that other people can't do this? (That is, when writing code to distribute to others.)

I use Python 2.7.4. Here's an example of code that runs perfectly for me in the REPL:

> import Image
> x = Image.new("RGB",(32,32),"blue)
> x.save("test.png","PNG")
Newb
  • 2,810
  • 3
  • 21
  • 35

2 Answers2

2

Looks like when you "import Image" directly, what is really going on inside that Image module is: "from PIL.Image import *", which copies all of the names from the PIL.Image module into the Image module.

Take a look at the source:

In [1]: import Image

In [2]: Image??
Type:        module
String form: <module 'Image' from '/usr/lib/python2.7/dist-packages/PILcompat/Image.pyc'>
File:        /usr/lib/python2.7/dist-packages/PILcompat/Image.py
Source:      from PIL.Image import *
jsw
  • 2,173
  • 2
  • 13
  • 14
0

You can use import in several different ways.

Here is a nice article on the common variations of import (you can do even do this in other ways too)

Gary Walker
  • 8,831
  • 3
  • 19
  • 41