1from importlib.abc import Loader 2from types import ModuleType 3from typing import cast 4 5 6def import_module(name: str, path: str) -> ModuleType: 7 import importlib.util 8 9 spec = importlib.util.spec_from_file_location(name, path) 10 assert spec is not None 11 module = importlib.util.module_from_spec(spec) 12 cast(Loader, spec.loader).exec_module(module) 13 return module 14