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 of get_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 the lazy_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

schrodinger.utils.imputils.to_module_spec(module)

Given a module, return a _ModuleSpec object that can be used to re-import the module using from_module_spec. The module spec may be treated as a 2-tuple of a string and a file path. If the module is in the Schrodinger build, the dotted name will be used. Otherwise, a script path will be used.

The script path will be suitable to be referenced from a job backend, but this function will not copy the script to the job directory.

schrodinger.utils.imputils.from_module_spec(spec)

Given a _ModuleSpec object created by to_module_spec, return the module.