[Python-Dev] Extensible library packages

M.-A. Lemburg mal@lemburg.com
Tue, 11 Apr 2000 18:41:26 +0200


"Andrew M. Kuchling" wrote:
> 
> For 1.6, the XML-SIG wants to submit a few more things, mostly a small
> SAX implementation.  This currently lives in xml.sax.*.  There are
> other subpackages around such as xml.dom, xml.utils, and so forth, but
> those aren't being proposed for inclusion (too large, too specialized,
> or whatever reason).
> 
> The problem is that, if the Python standard library includes a package
> named 'xml', that package name can't be extended by add-on modules
> (unless they install themselves into Python's library directory, which
> is evil).  Let's say Sean McGrath or whoever creates a new subpackage;
> how can he install it so that the code is accessible as xml.pyxie?

You could make use of the __path__ trick in packages and
then redirect the imports of subpackages to look in some
predefined other areas as well (e.g. a non-package dir
.../site-packages/xml-addons/).
 
Here is how I do this in the compatibility packages for
my mx series:

DateTime/__init__.py:

# Redirect all imports to the corresponding mx package
def _redirect(mx_subpackage):
    global __path__
    import os,mx
    __path__ = [os.path.join(mx.__path__[0],mx_subpackage)]
_redirect('DateTime')


... Greg won't like this, but __path__ does have its merrits ;-)

-- 
Marc-Andre Lemburg
______________________________________________________________________
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/