Return module 'modname', but with its contents loaded "on
demand"
This function returns 'sys.modules[modname]', if present. Otherwise
it creates a 'LazyModule' object for the specified module, caches it in
'sys.modules', and returns it.
'LazyModule' is a subclass of the standard Python module type, that
remains empty until an attempt is made to access one of its attributes.
At that moment, the module is loaded into memory, and any hooks that were
defined via 'whenImported()' are invoked.
Note that calling 'lazyModule' with the name of a non-existent or
unimportable module will delay the 'ImportError' until the moment access
is attempted. The 'ImportError' will occur every time an attribute
access is attempted, until the problem is corrected.
This function also takes an optional second parameter, 'relativePath',
which will be interpreted as a '/'-separated path string relative to
'modname'. If a 'relativePath' is supplied, the module found by
traversing the path will be loaded instead of 'modname'. In the path,
'.' refers to the current module, and '..' to the current module's
parent. For example:
fooBaz = lazyModule('foo.bar','../baz')
will return the module 'foo.baz'. The main use of the 'relativePath'
feature is to allow relative imports in modules that are intended for use
with module inheritance. Where an absolute import would be carried over
as-is into the inheriting module, an import relative to '__name__' will
be relative to the inheriting module, e.g.:
something = lazyModule(__name__,'../path/to/something')
The above code will have different results in each module that
inherits it.
(Note: 'relativePath' can also be an absolute path (starting with
'/'); this is mainly useful for module '__bases__' lists.)
|