Skip to main content

A simple lazy module loader for python

Project description

lazy-loading

lazy-loading is a simple lazy module loader for python.

Usage:

First, you'll need to import lazy-loading.

To lazily load a package, use:

lazy-loading.lazyload("package")

This will inject "package" into your global variables, meaning you can use package like usual.

[!TIP] If you don't want to inject into globals, you can set the inject parameter as such:

package = lazy-loading.lazyload("package", False)

lazyload will return the same LazyModule object it would inject into your global variables.

Note: lazyload will return the object even if inject is True.

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.

IDE autocompletion & type-checking

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 package2

This 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.

Limitations:

Partial lazy loading

Partial lazy loading (e.g. from package import symbol) is not supported. Accessing an attribute or function will automatically import the entire module.

Relative imports

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")

Lazily-loaded module dependencies

Imports called by lazily-loaded modules will not be lazy-loaded (unless the module uses lazy loading itself.)

Global name injection

In unusual contexts, global name injection may fail. To prevent this, you can assign the returned value of lazyload to a variable and possibly disable injection.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lazy_loading-0.1.0.post1.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lazy_loading-0.1.0.post1-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file lazy_loading-0.1.0.post1.tar.gz.

File metadata

  • Download URL: lazy_loading-0.1.0.post1.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.17 {"installer":{"name":"uv","version":"0.9.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"43","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for lazy_loading-0.1.0.post1.tar.gz
Algorithm Hash digest
SHA256 713eb745a2727039b6f9a28e0b3444fa50698d5bbbc7b86c8deed5ec25803f33
MD5 c98778550ef0ba5f827101ed6b48c35d
BLAKE2b-256 fb4090e08b23adf67084e169fba05447aa80eb5eaec3c6c1afd21a3837e9e077

See more details on using hashes here.

File details

Details for the file lazy_loading-0.1.0.post1-py3-none-any.whl.

File metadata

  • Download URL: lazy_loading-0.1.0.post1-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.17 {"installer":{"name":"uv","version":"0.9.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"43","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for lazy_loading-0.1.0.post1-py3-none-any.whl
Algorithm Hash digest
SHA256 ccec31281ae39ef993f1774d1cf636fcf40c98e87956dfbd5c77283780a20561
MD5 4c9dfc6f5a58c4be60d6d5435b76be41
BLAKE2b-256 582f810f86d74432ed2c549dbad92592d5afeb7df02f6291ca62274eb19f7b5b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page