473,320 Members | 2,097 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
imported.

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...

Thanks,

Scott Huey

Apr 27 '06 #1
1 2065

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

<snip>

__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>
__init__.py
<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

else:
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: David Mertz | last post by:
At the suggestion of one of my correspondents, I slightly reluctantly implemented an RSS feed for my website/writing. It is perhaps a bit crude so far, but maybe I'll spiff it up. The RSS also...
0
by: Roy Smith | last post by:
I had a class called rule and a subclass of rule, called bgpRule, in a file called bgp.py. I decided to move the rule class to its own file, called rule.py. The original file did "import rule"....
1
by: Åâãåíèé Êîñåíêî | last post by:
Hi! I make my first steps in Tkinter/Tix programming. The next program is work well (really, _almost_ well): import Tkinter root = Tkinter.Tk() widget = Tkinter.Button(root)
7
by: Michele Simionato | last post by:
So far, I have not installed Prothon, nor I have experience with Io, Self or other prototype-based languages. Still, from the discussion on the mailing list, I have got the strong impression that...
4
by: Martin Maney | last post by:
I've been to the cookbook (both forms, since they're sometimes usefully different), but that gave rise to more questions than answers. Heck, let me pull some of them up while I'm at it. ...
14
by: Axel Straschil | last post by:
Hello! Im working with new (object) classes and normaly call init of ther motherclass with callin super(...), workes fine. No, I've got a case with multiple inherance and want to ask if this...
2
by: Johnny Lee | last post by:
Here is the source: #! /bin/python #@brief This is a xunit test framework for python, see TDD for more details class TestCase: def setUp(self): print "setUp in TestCase"
6
by: seb | last post by:
Hi, I am using pygtk for the first times. I am wondering what would be the best "pattern" to interface pygtk with a thread. The thread is collecting informations (over the network for...
8
by: Viktor | last post by:
Can somebody give me an explanation what happened here (or point me to some docs)? Code: HMMM = None def w(fn): print 'fn:', id(fn) HMMM = fn
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.