Skip to content

import_object

faststream.cli.utils.imports.import_object #

import_object(module, app)

Import an object from a module.

Source code in faststream/cli/utils/imports.py
def import_object(module: Path, app: str) -> object:
    """Import an object from a module."""
    spec = spec_from_file_location(
        "mode",
        f"{module}.py",
        submodule_search_locations=[str(module.parent.absolute())],
    )

    if spec is None:  # pragma: no cover
        raise FileNotFoundError(module)

    mod = module_from_spec(spec)
    loader = spec.loader

    if loader is None:  # pragma: no cover
        raise SetupError(f"{spec} has no loader")

    loader.exec_module(mod)

    try:
        obj = getattr(mod, app)
    except AttributeError as e:
        raise FileNotFoundError(module) from e

    return obj