Skip to content

Importer

Utilities for discovering and importing modules within a package — used internally to find your project's models, extensions, and generators.

grace.importer

import_package_modules

import_package_modules(package, shallow=True)

Import all modules in the package and yield them in order.

PARAMETER DESCRIPTION
package

The package to import modules from.

TYPE: ModuleType

shallow

Whether to import only the top-level package (default: True).

TYPE: bool DEFAULT: True

Source code in grace/importer.py
11
12
13
14
15
16
17
18
19
20
21
22
def import_package_modules(
    package: ModuleType, shallow: bool = True
) -> Generator[ModuleType, None, None]:
    """Import all modules in the package and yield them in order.

    :param package: The package to import modules from.
    :type package: ModuleType
    :param shallow: Whether to import only the top-level package (default: True).
    :type shallow: bool
    """
    for module in find_all_importables(package, shallow):
        yield import_module(module)

find_all_importables

find_all_importables(package, shallow=True)

Find importable modules in the project and return them in order.

PARAMETER DESCRIPTION
package

The package to search for importable.

TYPE: ModuleType

shallow

Whether to search only the top-level package (default: True).

TYPE: bool DEFAULT: True

Source code in grace/importer.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def find_all_importables(package: ModuleType, shallow: bool = True) -> Set[str]:
    """Find importable modules in the project and return them in order.

    :param package: The package to search for importable.
    :type package: ModuleType
    :param shallow: Whether to search only the top-level package (default: True).
    :type shallow: bool
    """
    return set(
        chain.from_iterable(
            _discover_importable_path(Path(p), package.__name__, shallow)
            for p in package.__path__
        )
    )