From 4877767cf92649a38ffda0fc590f2b92ba59f019 Mon Sep 17 00:00:00 2001 From: YunLiu <55491388+KumoLiu@users.noreply.github.com> Date: Wed, 14 Aug 2024 09:55:53 +0800 Subject: [PATCH] Fix module can not import correctly issue (#8015) Fixes #8014 ### Types of changes - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [x] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. --------- Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- monai/utils/module.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/monai/utils/module.py b/monai/utils/module.py index 251232d62f..1ac8140b39 100644 --- a/monai/utils/module.py +++ b/monai/utils/module.py @@ -13,7 +13,6 @@ import enum import functools -import importlib.util import os import pdb import re @@ -209,11 +208,13 @@ def load_submodules( ): if (is_pkg or load_all) and name not in sys.modules and match(exclude_pattern, name) is None: try: + mod = import_module(name) mod_spec = importer.find_spec(name) # type: ignore if mod_spec and mod_spec.loader: - mod = importlib.util.module_from_spec(mod_spec) - mod_spec.loader.exec_module(mod) + loader = mod_spec.loader + loader.exec_module(mod) submodules.append(mod) + except OptionalImportError: pass # could not import the optional deps., they are ignored except ImportError as e: