what is __init__.py used for?

Terry Hancock hancock at anansispaceworks.com
Tue Jul 5 12:08:00 EDT 2005


On Tuesday 05 July 2005 06:39 am, John Roth wrote:
> The real kicker here is that when I say that the
> first module will be completely empty, it's not
> quite true. First, it will have some standard
> identifiers that all modules have, and second
> it will have anything you put into the __init__.py
> module file.

It's not at all uncommon, for example, for __init__.py
to contain a set of imports:

from SubMod1 import *
import SubMod2 as sub

etc,

this is one way to create the "interface" that the package
will present to you when you import it.

> This last is an advanced feature, and
> you're well advised to stay away from it until
> you've got a lot more experiance.

Not until you are writing Python packages, anyway.
But it's probably a good idea to know what it does
so that you can read what it's doing in packages you
use and/or study (and studying existing packages
is one of the best ways to learn once you've gotten
beyond the basic hurdle of writing simple programs).

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks  http://www.anansispaceworks.com




More information about the Python-list mailing list