1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
from importlib import (
import_module,
)
from typing import (
Any,
)
def import_string(dotted_path: str) -> Any:
"""
Import a variable using its path and name.
:param dotted_path: dotted module path and variable/class name
:return: the attribute/class designated by the last name in the path
:raise: ImportError, if the import failed
Source: django.utils.module_loading
"""
try:
module_path, class_name = dotted_path.rsplit(".", 1)
except ValueError:
msg = f"{dotted_path} doesn't look like a module path"
raise ImportError(msg)
module = import_module(module_path)
try:
return getattr(module, class_name)
except AttributeError:
msg = f'Module "{module_path}" does not define a "{class_name}" attribute/class'
raise ImportError(msg)
|