Mailing List Archive

Q: Why is __init__.py required
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'm not asking for this behavior to be changed. I like it the way
it is. I just want to know *why* it has to be that way. In what
way would the Python interpreter get confused if it didn't have
__init__.py to distinguish package directories from non-package
directories?

I've looked in the standard 1.5.2 docs and the FAQ and in Guido's
package essage and have searched Deja Vue.

Can anyone point me to an explanation?

It bugs me when I don't know why. My parents used to have to
explain things to me like why I couldn't jump off the roof of the
house.

Thanks in advance for help.

- Dave
Q: Why is __init__.py required [ In reply to ]
In article <7o2eeq$gs2@dfw-ixnews6.ix.netcom.com>,
G. David Kuhlman <dkuhlman@netcom.com> wrote:
>
>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.

Look at it this way: say you give Python the name of a directory. How
does Python know that the directory really is a package, rather than
some random directory? Like much else in Python (e.g. ":", "=="), this
straitjacket is designed to save you time by preventing stupid mistakes.
--
--- Aahz (@netcom.com)

Androgynous poly kinky vanilla queer het <*> http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6 (if you want to know, do some research)
Q: Why is __init__.py required [ In reply to ]
G. David Kuhlman writes:

> 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'm not asking for this behavior to be changed. I like it the way
> it is. I just want to know *why* it has to be that way. In what
> way would the Python interpreter get confused if it didn't have
> __init__.py to distinguish package directories from non-package
> directories?

import a.b becomes:
import a
if a is a package:
in this same directory, look for b
else
get this thing called b out of module a

> It bugs me when I don't know why. My parents used to have to
> explain things to me like why I couldn't jump off the roof of the
> house.

they-sure-blew-that-one-<wink>-ly y'rs

- Gordon