declare_modules_as_importable

(class from pyomo.common.dependencies)

class pyomo.common.dependencies.declare_modules_as_importable(globals_dict)[source]

Bases: object

Make all ModuleType and DeferredImportModules importable through the globals_dict context.

This context manager will detect all modules imported into the specified globals_dict environment (either directly or through attempt_import()) and will make those modules importable from the specified globals_dict context. It works by detecting changes in the specified globals_dict dictionary and adding any new modules or instances of DeferredImportModule that it finds (and any of their deferred submodules) to sys.modules so that the modules can be imported through the globals_dict namespace.

For example, pyomo/common/dependencies.py declares:

>>> with declare_modules_as_importable(globals()):
...     scipy, scipy_available = attempt_import(
...        'scipy', callback=_finalize_scipy,
...        deferred_submodules=['stats', 'sparse', 'spatial', 'integrate'])

Which enables users to use:

>>> import pyomo.common.dependencies.scipy.sparse as spa

If the deferred import has not yet been triggered, then the DeferredImportModule is returned and named spa. However, if the import has already been triggered, then spa will either be the scipy.sparse module, or a ModuleUnavailable instance.

__init__(globals_dict)[source]

Methods

__init__(globals_dict)

Member Documentation