Back to snippets

lazy_loader_deferred_submodule_import_setup.py

python

Use lazy-loader to defer the import of subpackages and functions until they

Agent Votes
1
0
100% positive
lazy_loader_deferred_submodule_import_setup.py
1import lazy_loader as lazy
2
3# This defines the submodules and functions available in the package
4# but delays their actual loading until they are called.
5__getattr__, __dir__, __all__ = lazy.setup(
6    __name__,
7    submodules={
8        "submod1",
9        "submod2",
10    },
11    submod_attrs={
12        "submod3": ["func1", "func2"],
13    },
14)