Skip to main content

LazImp, the package for lazy import of packages, modules and symboles

Project description

LazImp, The "don't wait for it..." package

LazImp is a package that allows lazy loading of python modules, packages and symboles for python 3.10+. This package allows you to load modules and packages only when the user use it. From dynamic import to "I only need this function, not the whole framework", the start-up time is speed-up and delayed over the execution of the software.

Using LazImp, you will reduce the memory usage and the start-up time of your software.

Example

First, you may have long loading or memory heavy modules to expose in your package api:

# package/heavy_module.py
print('Heavy module is loading...')
from time import sleep

sleep(10)
print('heavy_module loaded')

But instead of importing them directly, you can do a lazy import in the __init__.py:

# package/__init__.py of a package
import lazimp

math: lazimp.ModuleType
heavy_module: lazimp.ModuleType

__getattr__ = lazimp.lazy_import(
    'math',
    heavy_module='package',
)

Now, when you import the package:

# main.py
import package

print('Before access to heavy_module')
print(package.heavy_module)
print('After access to heavy_module')
print('Before access to math')
print(package.math)
print('After access to math')

And the output:

Before access to heavy_module
Heavy module is loading...
(wait 10 sec)
heavy_module loaded
<module 'heavy_module' from '...'>
After access to heavy_module
Before access to math
# math loaded
<module 'math' (built-in)>
After access to math

Without the lazy loading of heavy_module.py, the output would have been:

Heavy module is loading...
(wait 10 sec)
heavy_module loaded  # math loaded too
Before access to heavy_module
<module 'heavy_module' from '...'>
After access to heavy_module
Before access to math
<module 'math' (built-in)>
After access to math

Why this name: LazImp?

lazimp is a portmanteau of lazy and imp.

imp was the name of the module importlib before python 3.4 and its deprecation: What's new in Python 3.4: Deprecations in the Python API

The name could have been lazy_import but the imp part was preferred as a reference to the original module and because an imp with sunglasses and cocktail is cool 😎, and lazy shorten because it sounded better.

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

lazimp-0.0.5.tar.gz (3.8 kB view hashes)

Uploaded Source

Built Distribution

lazimp-0.0.5-py3-none-any.whl (4.0 kB view hashes)

Uploaded Python 3

Supported by

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