Import Helpers

Module: polygraphy.mod

LATEST_VERSION = 'latest'

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

lazy_import(name, log=True, version=None)[source]

Lazily import a module.

If the POLYGRAPHY_AUTOINSTALL_DEPS environment variable 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.

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

  • version (str) – The preferred version of the package, formatted as a version string. For example, '>=0.5.0' or '==1.8.0'. Use LATEST_VERSION to indicate that the latest version of the package is preferred.

Returns

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

Return type

LazyModule

has_mod(lazy_mod, with_attr='__version__')[source]

Checks whether a module is available.

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

  • with_attr (str) – The name of an attribute to check for. This helps distinguish mock modules from real ones.

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