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
andDeferredImportModules
importable through theglobals_dict
context.This context manager will detect all modules imported into the specified
globals_dict
environment (either directly or throughattempt_import()
) and will make those modules importable from the specifiedglobals_dict
context. It works by detecting changes in the specifiedglobals_dict
dictionary and adding any new modules or instances ofDeferredImportModule
that it finds (and any of their deferred submodules) tosys.modules
so that the modules can be imported through theglobals_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 namedspa
. However, if the import has already been triggered, thenspa
will either be thescipy.sparse
module, or aModuleUnavailable
instance.Methods
__init__
(globals_dict)Member Documentation