[py-dev] virtual namespace for plugins

Frederik Dohr fdg001 at gmx.net
Wed Nov 11 13:11:47 CET 2009


Holger and I had a conversation about virtual namespaces on IRC.
We're using this to have a common namespace for plugins*, and Holger 
expressed interest in using this approach as well.

The goal is to use a common package name (here: "foo") for multiple 
independent modules or packages ("alpha" and "bravo" - to be accessed as 
foo.alpha and foo.bravo, respectively).

It's actually rather simple:
The individual components must be contained in a directory named after 
the virtual namespace, with an __init__.py containing nothing but the 
following statement:
     __import__("pkg_resources").declare_namespace(__name__)
To make Python aware of the local package (e.g. for running tests out of 
the repository), something like the following is required in a 
root-level __init__.py:
     path = os.path.abspath(VIRTUAL_NAMESPACE)
     sys.modules[VIRTUAL_NAMESPACE].__dict__["__path__"].insert(0, path)

This is illustrated here:
     http://gist.github.com/231862

Disclaimer: I'm far from being an expert, just sharing what we've 
learned so far (it's an ongoing effort).


-- F.


* 
http://cdent.tumblr.com/post/216241761/python-namespace-packages-for-tiddlyweb



More information about the Pytest-dev mailing list