How modules work in Python

Dave Angel davea at davea.name
Sun Nov 16 17:39:32 EST 2014


Abdul Abdul <abdul.sw84 at gmail.com> Wrote in message:
> Hello,
> 
> I'm new to Python, and just have a small question, and thought you might have an idea on it.

You should start any new thread with a specification of the
 versions of software you're asking about.  Since you didn't, 
 I'll assume python version 2.7, PIL version 1.7, and Linux Ubuntu
  version 14.04. 

> 
> I came across the following example that uses the Python Imaging Library (PIL):

> 
> from PIL import Image
> img = Image.open('xyz.jpg')
> 
> I know that PIL is a module. And, I think that Image is also a module, especially we are importing it.

PIL is a package, whuch means it's a module containing other
 modules.,  and contains modules such as Image. But that syntax
 "from PIL import Image" doesn't tell you that. Any type of name
 defined in module PIL can be retrieved by the from
 syntax.

The way I can tell is either read the docs, or ask in the interpreter. 
from PIL import Image
print type (Image)

<type 'module'>

That from syntax is roughly equivalent to
import PIL
Image = PIL.Image

And the leading capital I would have made me guess it was a class.
 Thus I checked, using type ()

> 
> I also understood the Image,open() part, as it appears we are using the method open() from the Image module.

Modules don't have methods. open is an ordinary function in the module.

> 
> My question is, where did PIL go here?

I don't understand the question.  By using the from syntax, you
 avoided having PIL in your namespace. If you wanted, you could
 have said. import PIL.  And used PIL.Image.open()

 Can a module have another module inside it?
> 
Yes

> 
> 


-- 
DaveA




More information about the Python-list mailing list