[Python-ideas] Implicit submodule imports

Daniel Holth dholth at gmail.com
Sat Sep 27 18:02:26 CEST 2014


About apipkg...

When using apipkg, you define your module's API in one package, and
implement it in another:

# mypkg/__init__.py
import apipkg
apipkg.initpkg(__name__, {
    'path': {
        'Class1': "_mypkg.somemodule:Class1",
        'clsattr': "_mypkg.othermodule:Class2.attr",
    }
}

apipkg replaces sys.modules[mypkg] with a subclass of ModuleType.
Anything in the apipkg is exposed under an alias and lazily imported
on first use, including submodules.

I've really enjoyed using it. It lets me think about the API as a
separate entity from the implementation, and it lets me delay a slow
import until during the first method call, for much more pleasing
interactive tinkering.


More information about the Python-ideas mailing list