__init__.py in packages

G

Gary Wilson Jr

I'm creating a python package foo.

What is intended use for __init__.py files?
Well, I found this: http://www.python.org/doc/essays/packages.html
From what I can gather it is for initialization of the package when doing an
import, but I would really like to see an example or situation that makes good
use of the __init__.py file.

Looking at the distutils __init__.py file, it only defines things like
__version__. However, looking at the logging __init__.py file, it is a
1196-line monster with functions and classes defined throughout.

What is the RightThing?

Should I only define things in __init__.py that need to be defined when
importing a subpackage and/or module of package foo?

Is it ok for me to define classes in foo/__init__.py?
Whereby I could do something like:

from foo import MyClass

Or is is better if I were to put these classes and/or functions in foo/core.py?
Whereby I would do something like:

from foo.core import MyClass
 
F

F. Petitjean

Le Wed, 08 Jun 2005 10:34:38 -0500, Gary Wilson Jr a écrit :
I'm creating a python package foo.

What is intended use for __init__.py files?
Well, I found this: http://www.python.org/doc/essays/packages.html
import, but I would really like to see an example or situation that makes good
use of the __init__.py file.

Looking at the distutils __init__.py file, it only defines things like
__version__. However, looking at the logging __init__.py file, it is a
1196-line monster with functions and classes defined throughout.

What is the RightThing?

The RightThing is to not name your package "foo" :)
from foo import MyClass

Or is is better if I were to put these classes and/or functions in foo/core.py?
Whereby I would do something like:

from foo.core import MyClass

Naming matters. First find good names, second write down good
docstrings. In case you have difficulties to find good names, read "How
to write unmaintainable code" and you end up with :
from Mary.Poppins import Pinochio # the electromagnetics simulator.
 
J

Jarek Zgoda

Gary Wilson Jr napisa³(a):
What is intended use for __init__.py files?
Well, I found this: http://www.python.org/doc/essays/packages.html
import, but I would really like to see an example or situation that makes good
use of the __init__.py file.

Looking at the distutils __init__.py file, it only defines things like
__version__. However, looking at the logging __init__.py file, it is a
1196-line monster with functions and classes defined throughout.

What is the RightThing?

I don't know The Right Thing, I just use __init__.py as namespace
shortcut -- everything you define there (functions, classes, names),
will be available at module level, not deeper.
Should I only define things in __init__.py that need to be defined when
importing a subpackage and/or module of package foo?

Is it ok for me to define classes in foo/__init__.py?
Whereby I could do something like:

from foo import MyClass

Or is is better if I were to put these classes and/or functions in foo/core.py?
Whereby I would do something like:

from foo.core import MyClass

It's mostly a matter of taste. And logic.
 
G

Greg Ewing

Gary said:
I would really like to see an example or situation that makes good
use of the __init__.py file.

I've attached a non-trivial example, from my
PyGUI package. It uses some trickery to switch
in one of a number of subdirectories of platform
specific code, and then imports a bunch of names
from submodules into the top-level package, so
the user can pretend he's just using a single
top-level module, e.g.

from GUI import Window

even though Window is actually defined in some
submodule.

This is a rather extreme example, though -- most
__init__.py files are much simpler!

--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg

_versions = [
("Carbon", "Mac"),
("gtk", "Gtk"),
]

from os import environ as _env
_platdir = _env.get("PYGUI_IMPLEMENTATION")
if not _platdir:
for _testmod, _platdir in _versions:
try:
__import__(_testmod)
break
except ImportError:
continue
else:
raise ImportError("Unable to find an implementation of PyGUI for this installation")

print "PyGUI: Using implementation:", _platdir

from os.path import join as _join
_here = __path__[0]
__path__.append(_join(_here, _platdir))
__path__.append(_join(_here, "Generic"))

from Version import version

from Actions import Action
from AlertFunctions import alert, alert2, alert3, \
stop_alert, note_alert, confirm, ask, confirm_or_cancel, ask_or_cancel
from Applications import Application, application
from Buttons import Button
from CheckBoxes import CheckBox
from Colors import Color, rgb
from Components import Component
from Dialogs import Dialog
from Documents import Document
from Events import Event
from Exceptions import Cancel
from FileDialogs import request_old_file, request_new_file
from Files import FileRef, DirRef
from Fonts import Font
from Containers import Frame
from Images import Image
from Labels import Label
from Menus import Menu
from MessageHandlers import MessageHandler
from ModalDialogs import ModalDialog
from Models import Model
from Pixmaps import Pixmap
from Properties import Properties, overridable_property
from RadioButtons import RadioButton
from RadioGroups import RadioGroup
from ScrollBars import ScrollBar
from ScrollFrames import ScrollFrame
import StdColors
import StdFonts
from Tasks import Task
from TextFields import TextField
##from TextModels import TextModel
##from TextViews import TextView
from Views import View
from Windows import Window
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,698
Latest member
LydiaHalle

Latest Threads

Top