MCPcopy Index your code
hub / github.com/Project-MONAI/MONAI / optional_import

Function optional_import

monai/utils/module.py:317–451  ·  view source on GitHub ↗

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. Args: module: name of the module to be imported. version: version string used by

(
    module: str,
    version: str = "",
    version_checker: Callable[..., bool] = min_version,
    name: str = "",
    descriptor: str = OPTIONAL_IMPORT_MSG_FMT,
    version_args: Any = None,
    allow_namespace_pkg: bool = False,
    as_type: str = "default",
)

Source from the content-addressed store, hash-verified

315
316
317def optional_import(
318 module: str,
319 version: str = "",
320 version_checker: Callable[..., bool] = min_version,
321 name: str = "",
322 descriptor: str = OPTIONAL_IMPORT_MSG_FMT,
323 version_args: Any = None,
324 allow_namespace_pkg: bool = False,
325 as_type: str = "default",
326) -> tuple[Any, bool]:
327 """
328 Imports an optional module specified by `module` string.
329 Any importing related exceptions will be stored, and exceptions raise lazily
330 when attempting to use the failed-to-import module.
331
332 Args:
333 module: name of the module to be imported.
334 version: version string used by the version_checker.
335 version_checker: a callable to check the module version, Defaults to monai.utils.min_version.
336 name: a non-module attribute (such as method/class) to import from the imported module.
337 descriptor: a format string for the final error message when using a not imported module.
338 version_args: additional parameters to the version checker.
339 allow_namespace_pkg: whether importing a namespace package is allowed. Defaults to False.
340 as_type: there are cases where the optionally imported object is used as
341 a base class, or a decorator, the exceptions should raise accordingly. The current supported values
342 are "default" (call once to raise), "decorator" (call the constructor and the second call to raise),
343 and anything else will return a lazy class that can be used as a base class (call the constructor to raise).
344
345 Returns:
346 The imported module and a boolean flag indicating whether the import is successful.
347
348 Examples::
349
350 >>> torch, flag = optional_import('torch', '1.1')
351 >>> print(torch, flag)
352 <module 'torch' from 'python/lib/python3.6/site-packages/torch/__init__.py'> True
353
354 >>> the_module, flag = optional_import('unknown_module')
355 >>> print(flag)
356 False
357 >>> the_module.method # trying to access a module which is not imported
358 OptionalImportError: import unknown_module (No module named 'unknown_module').
359
360 >>> torch, flag = optional_import('torch', '42', exact_version)
361 >>> torch.nn # trying to access a module for which there isn't a proper version imported
362 OptionalImportError: import torch (requires version '42' by 'exact_version').
363
364 >>> conv, flag = optional_import('torch.nn.functional', '1.0', name='conv1d')
365 >>> print(conv)
366 <built-in method conv1d of type object at 0x11a49eac0>
367
368 >>> conv, flag = optional_import('torch.nn.functional', '42', name='conv1d')
369 >>> conv() # trying to use a function from the not successfully imported module (due to unmatched version)
370 OptionalImportError: from torch.nn.functional import conv1d (requires version '42' by 'min_version').
371 """
372
373 had_exception = False
374 exception_str = ""

Callers 15

profiling.pyFile · 0.90
misc.pyFile · 0.90
enums.pyFile · 0.90
jupyter_utils.pyFile · 0.90
nvtx.pyFile · 0.90
dist.pyFile · 0.90
type_conversion.pyFile · 0.90
has_ampere_or_laterFunction · 0.90
trt_compiler.pyFile · 0.90
utils.pyFile · 0.90
_onnx_trt_compileFunction · 0.90

Calls 1

_LazyRaiseClass · 0.85

Tested by 15

__init__Method · 0.72
__init__Method · 0.72
has_cupyFunction · 0.72
test_onnx_saveFunction · 0.72
run_transformMethod · 0.72
test_filesMethod · 0.72
test_defaultMethod · 0.72
test_import_validMethod · 0.72
test_import_exactMethod · 0.72
test_import_methodMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…