schrodinger.utils.imputils module¶
Utility functions to import python modules. Always prefer loading from a location on sys.path.
Copyright Schrodinger, LLC. All rights reserved.
- schrodinger.utils.imputils.import_module_from_file(filename)¶
Import module from a file path. Returns the imported module object.
- schrodinger.utils.imputils.import_from_file(modulename, filename)¶
- schrodinger.utils.imputils.get_path_from_module(module)¶
Given a module, return a path string. The path string will either be in ‘.’ format if the module can be found in either the schrodinger scripts or modules directory or an absolute path if the module is defined in a python unittest module. Inverse function of
get_module_from_path
.- Parameters
module (module) – The module to export a path string for. To get the module of any arbitrary object, use
inspect.getmodule
.
- schrodinger.utils.imputils.get_module_from_path(module_path)¶
Given a module path string generated by
get_path_from_module
, return the corresponding module. Inverse function ofget_path_from_module
.- Parameters
module_path (str) – The path string describing the module to import
- schrodinger.utils.imputils.import_script(name, subdir=None, common=False)¶
Import the given script residing in mmshare/python/scripts as a module. If the script is not found in the scripts directory, an attempt is made to import the script from a subdirectory following our standard naming convention by replacing ‘_driver.py’ or ‘_backend.py’ with ‘_gui_dir’.
- Parameters
name (str) – The name of the script, including the .py extension
subdir (str) – The name of the subdirectory the script resides in - must be a path relative to mmshare/python/scripts. If given, this name will be used instead of attempting to derive a subdirectory name from the script name.
common (bool) – Import from python/common rather than python/scripts
- Return type
module
- Returns
The script imported as a module
- Raises
FileNotFoundError – If the script can’t be found
- schrodinger.utils.imputils.lazy_import(name)¶
Lazily import a module. The actual import will not happen until the module is first used. This can help to avoid performance bottlenecks (i.e. if a module is rarely used but the import slows down panel launching) or to avoid circular imports (although refactoring your code to avoid the circular import in the first place may be a better option).
For example:
from schrodinger.utils import imputils phase_markers = imputils.lazy_import("schrodinger.application.phase.phase_markers")
phase_markers
can now be used as if the line had said:import schrodinger.application.phase.phase_markers as phase_markers
but the actual import won’t occur until the module is first used (or if the module is imported normally somewhere else). Note that imports of higher level packages (i.e. the “schrodinger”, “schrodinger.application”, and “schrodinger.application.phase” packages for the example above) will not happen lazily. If any of those packages contain an
__init__.py
, then the__init__.py
will be executed during thelazy_import
call.- Parameters
name (str) – The name of the module to lazily import. This must be given as an absolute name, not a relative one (i.e. “schrodinger.application.phase.phase_markers”, not “.phase_markers”)
- Returns
A placeholder for the module
- Return type
ModuleType