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",
)
| 315 | |
| 316 | |
| 317 | def 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 = "" |
searching dependent graphs…