Source code for confwire.utils.misc

import warnings
from importlib import import_module


[docs]def import_modules_from_strings(imports, allow_failed_imports=False): """Import modules from the given list of strings. Args: imports (list | str | None): The given module names to be imported. allow_failed_imports (bool): If True, the failed imports will return None. Otherwise, an ImportError is raise. Default: False. Returns: list[module] | module | None: The imported modules. Examples: >>> osp, sys = import_modules_from_strings( ... ['os.path', 'sys']) >>> import os.path as osp_ >>> import sys as sys_ >>> assert osp == osp_ >>> assert sys == sys_ """ if not imports: return single_import = False if isinstance(imports, str): single_import = True imports = [imports] if not isinstance(imports, list): raise TypeError(f"custom_imports must be a list but got type {type(imports)}") imported = [] for imp in imports: if not isinstance(imp, str): raise TypeError(f"{imp} is of type {type(imp)} and cannot be imported.") try: imported_tmp = import_module(imp) except ImportError: if allow_failed_imports: warnings.warn(f"{imp} failed to import and is ignored.", UserWarning) imported_tmp = None else: raise ImportError imported.append(imported_tmp) if single_import: imported = imported[0] return imported