what is __init__.py used for?
Walter Dörwald
walter at livinglogic.de
Tue Jul 5 06:25:28 EDT 2005
zelzel.zsu at gmail.com wrote:
> I am a new learner of Python Programming Language.
> Now. I am reading a book.
> In the section relating to module, I see an example.
> the directory tree looks like below:
> root\
> system1\
> __init__.py
> utilities.py
> main.py
> other.py
> system2\
> __init__.py
> utilities.py
> main.py
> other.py
> system3\ # Here or elsewhere
> __init__.py # Your new code here
> myfile.py
>
> question
> ==========
> I was wonderring ... what is the __init__.py used for ?
> This question may seems to be stupid for an expert.
> But, if you can give the answer, it will be helpful for me.
If the root directory is on the Python search path, you can do "import
system2.other" or "from system2 import other", to import the other.py
module. But you can also do "import system2". This means that the source
code for the system2 module has to live somewhere. __init.py inside the
directory with the same name is this "somewhere". Without this
__init__.py inside the system2 directoy you couldn't import other.py
because Python doesn't know where the source code for system2 lives and
refuses to treat system2 as a package.
Hope that helps,
Walter Dörwald
More information about the Python-list
mailing list