[CentralOH] Namespace package structure

Mark Erbaugh mark at microenh.com
Sat Jun 11 13:35:24 CEST 2011

Yet another question from "Expert Python Programming"

One of the things discussed in the book is namespace packages.  The book gives a link to setuptools documentation at


that includes the following statement:

> You must NOT include any other code and data in a namespace package's __init__.py. Even though it may appear to work during development, or when projects are installed as .egg files, it will not work when the projects are installed using "system" packaging tools -- in such cases the __init__.py files will not be installed, let alone executed.

Yet, in the book, the author shows adding code to the __init__.py file at the deepest level of the package.  The namespace package created is atomisator.parser and the __init__.py file in question is atomisator/parser/__init__.py.  I have a couple of questions about this.

Is it good practice to add code to the __init__.py file?  This is done because the api is used like: from atomisator.parser import parse. My tendency would have been to have a separate module, (i.e. parse.py) and then use from atomisator.parser.parse import parse. 

Do the lowest level __init__.py file need the "magic" startup code to create a namespace package?  From the setuptools documentation:

> These __init__.py files must contain the line:
> __import__('pkg_resources').declare_namespace(__name__)
> You must include the declare_namespace() line in the __init__.py of every project that has contents for the namespace package in question, in order to ensure that the namespace will be declared regardless of which project's copy of __init__.py is loaded first. If the first loaded __init__.py doesn't declare it, it will never be declared, because no other copies will ever be loaded!)



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/mailman/private/centraloh/attachments/20110611/270d6585/attachment.html>

More information about the CentralOH mailing list