473,383 Members | 1,748 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,383 software developers and data experts.

__init__.py in packages

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
Jul 19 '05 #1
3 5199
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
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?


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.

--
Cent fois sur le métier, remettez votre ouvrage.
Polissez-le et le repolissez
Boileau Despréaux.
Jul 19 '05 #2
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
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?


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.

--
Jarek Zgoda
http://jpa.berlios.de/ | http://www.zgodowie.org/
Jul 19 '05 #3
Gary Wilson Jr wrote:
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

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Robert Clayton | last post by:
In the PyKyra example programs what does the statement: "from PyKyra import *" I now assume that this represents a Package hierarchy and not the file "PyKyra.py" as there is none but for...
4
by: Christopher J. Bottaro | last post by:
Hello, I want to be able to say stuff like "import CJB.ClassA" and "import CJB.ClassB" then say "c = CJB.ClassA()" or "c = CJB.ClassB()". CJB will be a directory containing files "ClassA.py" and...
2
by: Peter Saffrey | last post by:
(apologies for starting a new thread - Google can't retrieve the other message for some reason) Yes, /usr/lib/python/site-packages is in sys.path. This series of commands should explain what I...
6
by: zelzel.zsu | last post by:
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\...
1
by: Kwikrick | last post by:
When calling str() on a sequence or dict object, the elements of the sequence/dict will be represented as if their __repr__ method was called. Why is this? Wouldn't it be more consistent when...
1
by: tyler | last post by:
I've written a small python extension but I'm having difficulty loading it at runtime. The source for my extension is a module which is a member of a package is organized as follows. ...
3
by: Srikanth | last post by:
Hi, I am learning about Python packages and I am getting an ImportError but I can't figure out the reason why. I have three modules and they are stored in a directory hierarchy as: my_apps...
1
by: Johannes Bauer | last post by:
Hello group, I'm having a seemingly simple problem. I want to generate a hierarchy of modules, like this one: GenerationScripts/ GenerationScripts/dhcp GenerationScripts/bind9 And the...
7
by: Daniel | last post by:
Hello, I'm writing some unit tests for my python software which uses packages. Here is the basic structure: mypackage __init__.py module1 __init__.py mod1.py
1
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.