Skip to content

Import Utils

reload(module)

Reload an existing module or import it if not already imported.

If the specified module is already present in the global namespace, it will be reloaded. Otherwise, the module will be imported.

Parameters:

Name Type Description Default
module str

The name of the module to reload or import.

required

Returns:

Type Description
None

None

Source code in mbodied/utils/import_utils.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
def reload(module: str) -> None:
    """Reload an existing module or import it if not already imported.

    If the specified module is already present in the global namespace,
    it will be reloaded. Otherwise, the module will be imported.

    Args:
        module (str): The name of the module to reload or import.

    Returns:
        None
    """
    if module in globals():
        return importlib.reload(globals()[module])
    return importlib.import_module(module)

smart_import(name, mode=None)

Import a module with optional lazy loading.

This function imports a module by name. If the module is already present in the global namespace, it will return the existing module. If the mode is set to "lazy", the module will be loaded lazily, meaning that the module will only be fully loaded when an attribute or function within the module is accessed.

Parameters:

Name Type Description Default
name str

The name of the module to import.

required
mode Literal['lazy'] | None

If "lazy", the module will be imported using a lazy loader. Defaults to None.

None

Returns:

Name Type Description
ModuleType ModuleType

The imported module.

Raises:

Type Description
NameError

If the module cannot be found or imported.

Source code in mbodied/utils/import_utils.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def smart_import(name: str, mode: Literal["lazy"] | None = None) -> ModuleType:
    """Import a module with optional lazy loading.

    This function imports a module by name. If the module is already
    present in the global namespace, it will return the existing module.
    If the `mode` is set to "lazy", the module will be loaded lazily,
    meaning that the module will only be fully loaded when an attribute
    or function within the module is accessed.

    Args:
        name (str): The name of the module to import.
        mode (Literal["lazy"] | None, optional): If "lazy", the module will
            be imported using a lazy loader. Defaults to None.

    Returns:
        ModuleType: The imported module.

    Raises:
        NameError: If the module cannot be found or imported.
    """
    if name in globals():
        return globals()[name]
    if mode == "lazy":
        spec = find_spec(name)
        if spec is None:
            msg = f"Module `{name}` not found"
            raise NameError(msg)
        loader = LazyLoader(spec.loader)
        spec.loader = loader
        module = module_from_spec(spec)
        sys.modules[name] = module
        loader.exec_module(module)
        return module

    try:
        return importlib.import_module(name)
    except ImportError as e:
        msg = f"Module {name} not found"
        raise NameError(msg) from e