Skip to content

Modules

MODULE UTILITIES

get_loaded_module(module_path, module_name, recache=False)

Lookup a loaded module by its filepath and reload it. If not found, load the module fresh. @param module_path: File path to the module. @param module_name: Name to give the module if loading it fresh. @param recache: If True, reload the module before returning it. @return: True if loaded, otherwise False.

Source code in src/utils/modules.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def get_loaded_module(module_path: Path, module_name: str, recache: bool = False) -> ModuleType:
    """
    Lookup a loaded module by its filepath and reload it. If not found, load the module fresh.
    @param module_path: File path to the module.
    @param module_name: Name to give the module if loading it fresh.
    @param recache: If True, reload the module before returning it.
    @return: True if loaded, otherwise False.
    """
    # Check if this module has been imported before
    if module_name in sys.modules:
        if recache:
            del sys.modules[module_name]
            return get_new_module(module_path, module_name)
        return sys.modules[module_name]

    # Model not loaded, load it now
    return get_new_module(module_path, module_name)

get_new_module(module_path, module_name)

Loads a module from a given path with assigned name. @param module_path: Path to module file. @param module_name: Name of the loaded module. @return: Loaded module.

Source code in src/utils/modules.py
29
30
31
32
33
34
35
36
37
38
39
40
def get_new_module(module_path: Path, module_name: str) -> ModuleType:
    """
    Loads a module from a given path with assigned name.
    @param module_path: Path to module file.
    @param module_name: Name of the loaded module.
    @return: Loaded module.
    """
    spec = spec_from_file_location(module_name, module_path)
    module = module_from_spec(spec)
    spec.loader.exec_module(module)
    sys.modules[module_name] = module
    return module