lazy-loading is a simple lazy module loader for python.
First, you'll need to import lazy-loading.
To lazily load a package, use:
package = lazy-loading.lazyload("package")You can lazily load submodules of your package:
lazy-loading.lazyload("mypacakge.submodule")Important
Relative imports (e.g. .submodule) are not supported. You need to use absolute module paths.
To get type-checking and autocompletion, you need to import all lazy-loaded packages
if typing.TYPE_CHECKING is True:
from lazy-loading import lazyload
from typing import TYPE_CHECKING
lazyload("package1")
lazyload("package2")
if TYPE_CHECKING:
import package1
import package2This will only run if a type-checker or linter is checking your code and will not cause packages to not lazy load in normal contexts.
Partial lazy loading (e.g. from package import symbol) is not supported.
Accessing an attribute or function will automatically import the entire module.
As mentioned previously, relative imports are not supported. You can lazy-load modules in your script's directory:
lazy-loading.lazyload("module")If you are building a package:
lazy-loading.lazyload("package.module")
Imports called by lazily-loaded modules will not be lazy-loaded (unless the module uses lazy loading itself.)
Global name injection is not supported. You'll need to set a variable to the returned lazy-loaded package. It will replace itself with the lazy-loaded package when a symbol is accessed.