unifying many packages under one name

Peter Otten __peter__ at web.de
Fri Aug 24 01:47:15 EDT 2007


Eric S. Johansson wrote:

> I have a collection of packages and I want to put them under single
> unifying
> name.  my goal is to reduce namespace pollution and make all these
> packages accessible as 'import vvv.aaa'. In more detail, if I have
> packages 'aaa' and 'bbb', what do I do to put those packages under
> unifying name such as 'vvv'? the only way I can see to do it is to create
> 'vvv' as a directory with its own __init__.py but I'm not sure that were
> work right either.

Of course you could make a module vvv.py with lots of import statements

import aaa
import bbb
...
import zzz

but then all submodules are imported eagerly with

import vvv

And then there's the hack

import sys

class Importer(object):
    def __getattr__(self, name):
        module = __import__(name) # you can do anything here
        setattr(self, name, module)
        return module

sys.modules["vvv"] = Importer()

Peter



More information about the Python-list mailing list