import package w/ all subpackages?

Carl Banks imbosol-1049044895 at aerojockey.com
Sun Mar 30 19:48:14 CEST 2003


Your Mom wrote:
>    There doesn't seem to be a built-in way to import a package w/ all it's
> subpackages & modules.  Does anyone else see value in adding a statement
> like "import fully foolib"?

No, this is useless enough that regular Python code should do it :).
Mostly untested, but I hope you get the idea:

    import os

    def _import_submodules(m):
        if os.path.basename(m.__file__)[:8] == '__init__':
            for sym in getattr(m,'__all__',()):
                if not hasattr(m,sym):
                    _subimport_fully(m.__name__, sym)

    def _subimport_fully(prefix,name):
        _import_submodules(__import__(prefix,fromlist=name))

    def import_fully(name):
        m = __import__(name)
        _import_submodules(m)
        return m

-- 
CARL BANKS




More information about the Python-list mailing list