__init__.py

Fredrik Lundh fredrik at pythonware.com
Tue Nov 14 06:23:33 EST 2006


km wrote:

> what is the use of __init__.py file in a module dir ?

it tells Python that the directory is a package directory.  if you have

     mydir/foo/__init__.py
     mydir/foo/module.py

and mydir is on the path, you can do "import foo.module" or "from foo 
import module".  if you remove the __init__.py file, Python will no 
longer look for submodules inside that directory.

> Is it used to initialize variables  that could be shared across sub 
> modules  if set in __init__.py at root dir  of module ?

it's usually empty, or used to export selected portions of the package 
under more convenient names.  given the example above, the contents of 
the __init__ module can be accessed via:

     import foo

</F>




More information about the Python-list mailing list