Newbie question about importing modules.

Tim Roberts timr at probo.com
Sat Dec 18 14:20:53 EST 2010


cronoklee <cronoklee at gmail.com> wrote:
>
>Hey thanks for the help fellas. The links were helpful and the pyExe
>program looks great. I might well end up using this.
>
>I'm still a little confused as to how the directory structure works. PIL
>(http://www.pythonware.com/products/pil/#pil117), for example comes packed
>in a folder called Imaging-1.1.7 which contains a bunch of other 
>directories, one of which is PIL.

That is a source distribution.  It is not intended to be used as is -- it
must be installed first.  The installation process will build whatever
shared libraries might be required, and then move the directories and
folders into the site-packages directory of your Python installation.

In this case, it will create a subdirectory called site-packages/PIL.  The
"site-packages" directory is already on your Python path, so when you say

    from PIL import Image

the interpreter will scan through all of the directories in the path,
notice that one of them contains a folder called PIL that has the right
format for a module (meaning it has __init__.py), and will bring in
Image.py from that directory.

The PIL installer also creates a file called site-packages/PIL.pth which
contains the name "PIL".  Python looks for .pth files and adds those
directories to the module path at startup.  So, this would ALSO work:

    import Image

>Sorry for the stupidity - I'm coming from PHP where you just include path/to/script.php so this is a bit alien to me.

That certainly works for individual scripts, but even with PHP there are
customary central locations where complicated packages are installed.
-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-list mailing list