File tree Expand file tree Collapse file tree 1 file changed +63
-1
lines changed
Expand file tree Collapse file tree 1 file changed +63
-1
lines changed Original file line number Diff line number Diff line change 1- # LazImp
1+ # LazImp
2+
3+ LazImp is a package that allows lazy loading of python modules, packages and
4+ symboles for python 3.10+. This package allows you to load modules and packages
5+ only ** when** the user use it. From dynamic import to "I only need this
6+ function, not the whole framework", the start-up time is speed-up and delayed
7+ over the execution of the software.
8+
9+ ## Example
10+
11+ First, you may have long loading or memory heavy modules to expose in your
12+ package api:
13+
14+ ``` python
15+ # heavy_module.py
16+ from time import sleep
17+
18+ sleep(10 )
19+ print (' heavy_module loaded' )
20+ ```
21+
22+ But instead of importing them directly, you can do a lazy import in
23+ the ` __init__.py ` :
24+
25+ ``` python
26+ # __init__.py of a package
27+ import lazimp
28+
29+ heavy_module: lazimp.ModuleType
30+
31+ __getattr__ = lazimp.lazy_import({' heavy_module' })
32+ ```
33+
34+ Now, when you import the package:
35+
36+ ``` python
37+ # main.py
38+ import package
39+
40+ print (' Before access to heavy_module' )
41+ print (package.heavy_module)
42+ print (' After access to heavy_module' )
43+ ```
44+
45+ And the output:
46+
47+ ``` txt
48+ Before access to heavy_module
49+ (wait 10 sec)
50+ heavy_module loaded
51+ <module 'heavy_module' from '...'>
52+ After access to heavy_module
53+ ```
54+
55+ Without the lazy loading of ` heavy_module.py ` , the output would have been:
56+
57+ ``` txt
58+ (wait 10 sec)
59+ heavy_module loaded
60+ Before access to heavy_module
61+ <module 'heavy_module' from '...'>
62+ After access to heavy_module
63+ ```
You can’t perform that action at this time.
0 commit comments