470,599 Members | 1,427 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,599 developers. It's quick & easy.

What do you use __init__.py for?

I have just started learning about Python Packages. I know that a
directory must contains the '__init__.py' script to be considered a
Python package, and that this script is executed when the package is

But what other uses does the '__init__.py' script have? What do you
use it for?

I imagine it could provide, at the least, some helpful metadata about
the contents and dependencies of the package.

I'm just looking for ideas...


Scott Huey

Apr 27 '06 #1
1 1975

redefined.horiz...@gmail.com wrote:
But what other uses does the '__init__.py' script have? What do you
use it for?


__init__.py is used for initialization of the package - similar to
__init__() in a function or class declaration.

One example would be if you create a package with generic database
methods - you can, in the __init__.py file, configure which actual
database drivers are used but only have to write your routines to the
generic setup.

Module structure:
<db directory>
<db directory>/my_bsddb
--> files for bsddb access go here

<db directory>/my_postgresql
--> files for postgresql access go here
Example __init__.py (pseudo coded, not python coded):
if (configure_database == "berkelyDB"):
import my_bsddb as db

elif (configure_database == "postgresql"):
import my_postgresql as db

log("DB config error - no valid datbase selected")


Then, in your routines, you only need to call db.<method/function>

Apr 27 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by David Mertz | last post: by
reply views Thread by Roy Smith | last post: by
7 posts views Thread by Michele Simionato | last post: by
4 posts views Thread by Martin Maney | last post: by
14 posts views Thread by Axel Straschil | last post: by
2 posts views Thread by Johnny Lee | last post: by
8 posts views Thread by Viktor | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.