nvflare.fuel.utils.import_utils module

Part of code is Adapted from from https://github.com/Project-MONAI/MONAI/blob/dev/monai/utils/module.py#L282

exception LazyImportError[source]

Bases: ImportError

Could not import APIs from an optional dependency.

check_version(that_pkg, version: str = '', op: str = '==') bool[source]

compare module version with provided version

get_module_version(this_pkg)[source]
get_module_version_str(the_module)[source]
optional_import(module: str, op: str = '==', version: str = '', name: str = '', descriptor: str = '{}', allow_namespace_pkg: bool = False) Tuple[Any, bool][source]

Imports an optional module specified by module string. Any importing related exceptions will be stored, and exceptions raise lazily when attempting to use the failed-to-import module. :param module: name of the module to be imported. :param op: version op it should be one of the followings: ==, <=, <, >, >= :param version: version string of the module if specified, it is used to the module version such :param that is satisfy the condition: <module>.__version__ <op> <version>. :param name: a non-module attribute (such as method/class) to import from the imported module. :param descriptor: a format string for the final error message when using a not imported module. :param allow_namespace_pkg: whether importing a namespace package is allowed. Defaults to False.

Returns:

The imported module and a boolean flag indicating whether the import is successful.

Examples::
>>> torch, flag = optional_import('torch')
>>> print(torch, flag)
<module 'torch' from '/..../lib/python3.8/site-packages/torch/__init__.py'> True
>>> torch, flag = optional_import('torch', '1.1')
>>> print(torch, flag)
<module 'torch' from 'python/lib/python3.6/site-packages/torch/__init__.py'> True
>>> the_module, flag = optional_import('unknown_module')
>>> print(flag)
False
>>> the_module.method  # trying to access a module which is not imported
OptionalImportError: import unknown_module (No module named 'unknown_module').
>>> torch, flag = optional_import('torch', '42')
>>> torch.nn  # trying to access a module for which there isn't a proper version imported
OptionalImportError: import torch (requires version=='42').
>>> conv, flag = optional_import('torch.nn.functional', ">=", '1.0', name='conv1d')
>>> print(conv)
<built-in method conv1d of type object at 0x11a49eac0>
>>> conv, flag = optional_import('torch.nn.functional', ">=", '42', name='conv1d')
>>> conv()  # trying to use a function from the not successfully imported module (due to unmatched version)
OptionalImportError: from torch.nn.functional import conv1d (requires version>='42').