Mailing List Archive

Why is __init__.py required
G. David Kuhlman <dkuhlman@netcom.com> wrote in message
news:7o2eeq$gs2@dfw-ixnews6.ix.netcom.com...
> I have looked and looked for this. Whenever I put an empty
> __init__.py in a package, I ask myself why I have to do this. I
> read Guido's article on packages and it told me that I have to put
> an __init__.py in a directory in order for the directory to be
> recognized as a package. But it doesn't say why.

I just noticed earlier today that you can put code *in* an __init__.py file,
which makes that file seem a little more useful! For example, see the source
of mxTextTools ( http://starship.python.net/~lemburg/mxTextTools.html ) or
the XML package (and probably lots of other packages). It's a feature I've
never seen explained.

--David Niergarth
Why is __init__.py required [ In reply to ]
David Niergarth <jdnier@execpc.com> wrote in message news:...

> It's a feature I've never seen explained.

Oops. From the Python 1.5.2 Tutorial:

... In the simplest case, __init__.py can just be an empty file, but it can
also execute initialization code for the package or set the __all__
variable, ...