Import Helpers

Module: polygraphy.mod

LATEST_VERSION = '==latest'

Indicates that the latest version of the package is preferred in lazy_import

lazy_import(name: str, log: Optional[bool] = None, pkg_name: Optional[str] = None, install_flags: Optional[List[str]] = None, requires: Optional[List[str]] = None)[source]

Lazily import a module.

If config.AUTOINSTALL_DEPS is set to 1, missing modules are automatically installed, and existing modules may be upgraded if newer versions are required.

Parameters
  • name (str) – The name of the module and optionally the preferred version of the package, formatted as a version string. For example, 'example_module>=0.5.0' or 'example_module==1.8.0'.

  • log (bool) – Whether to log information about the module. Defaults to True.

  • pkg_name (str) – The name of the package that provides this module, if it is different from the module name. Used only if automatic installation of dependencies is enabled.

  • install_flags (List[str]) – Additional flags to provide to the installation command. Used only if automatic installation of dependencies is enabled.

  • requires (List[str]) – Additional dependencies required by the module which are not specified as dependencies. This parameter should only be required when a module does not correctly specify dependencies. Defaults to [].

Returns

A lazily loaded module. When an attribute is first accessed, the module will be imported.

Return type

LazyModule

has_mod(modname)[source]

Checks whether a module is installed without importing the module.

Parameters

modname (str) – The name of the module to check.

Returns

Whether the module is installed.

Return type

bool

autoinstall(lazy_mod)[source]

If the config.AUTOINSTALL_DEPS is set to 1, automatically install or upgrade a module. Does nothing if autoinstallation is disabled.

Parameters

lazy_mod (LazyModule) – A lazy module, like that returned by lazy_import.

import_from_script(path, name)[source]

Imports a specified symbol from a Python script.

Parameters
  • path (str) – A path to the Python script. The path must include a ‘.py’ extension.

  • name (str) – The name of the symbol to import from the script.

Returns

The loaded symbol.

Return type

object