Re: what is __init__.py used for?
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Re: what is __init__.py used for?

From: Walter Dörwald <walter@livinglogic.de>
Date: Tue Jul 05 2005 - 12:25:28 CEST

zelzel.zsu@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
Received on Sat Oct 15 03:56:15 2005