Traverse the source of the module structure starting with module `basemod`, loading all packages plus all files if `load_all` is True, excluding anything whose name matches `exclude_pattern`.
(
basemod: ModuleType, load_all: bool = True, exclude_pattern: str = "(.*[tT]est.*)|(_.*)"
)
| 172 | |
| 173 | |
| 174 | def load_submodules( |
| 175 | basemod: ModuleType, load_all: bool = True, exclude_pattern: str = "(.*[tT]est.*)|(_.*)" |
| 176 | ) -> tuple[list[ModuleType], list[str]]: |
| 177 | """ |
| 178 | Traverse the source of the module structure starting with module `basemod`, loading all packages plus all files if |
| 179 | `load_all` is True, excluding anything whose name matches `exclude_pattern`. |
| 180 | """ |
| 181 | submodules = [] |
| 182 | err_mod: list[str] = [] |
| 183 | for importer, name, is_pkg in walk_packages( |
| 184 | basemod.__path__, prefix=basemod.__name__ + ".", onerror=err_mod.append |
| 185 | ): |
| 186 | if (is_pkg or load_all) and name not in sys.modules and match(exclude_pattern, name) is None: |
| 187 | try: |
| 188 | mod = import_module(name) |
| 189 | mod_spec = importer.find_spec(name) # type: ignore |
| 190 | if mod_spec and mod_spec.loader: |
| 191 | loader = mod_spec.loader |
| 192 | loader.exec_module(mod) |
| 193 | submodules.append(mod) |
| 194 | except OptionalImportError: |
| 195 | pass # could not import the optional deps., they are ignored |
| 196 | except ImportError as e: |
| 197 | msg = ( |
| 198 | f"\nError on import of {name}\n" |
| 199 | "\nMultiple versions of MONAI may have been installed?\n" |
| 200 | "Please see the installation guide: https://monai.readthedocs.io/en/stable/installation.html\n" |
| 201 | ) # issue project-monai/monai#5193 |
| 202 | raise type(e)(f"{e}\n{msg}").with_traceback(e.__traceback__) from e # raise with modified message |
| 203 | |
| 204 | return submodules, err_mod |
| 205 | |
| 206 | |
| 207 | def instantiate(__path: str, __mode: str, **kwargs: Any) -> Any: |
no test coverage detected
searching dependent graphs…