Importing and Lazy_import
To see the import function in pyforest module we can print the lazy_modules() function:
Python3
import pyforest print (lazy_imports()) |
Output:
So we can see in the above output what all modules are existing in the pyforest library which is available for use without importing multiple times.
This was just one line but if we had to import a lot of modules we will have to focus more on importing modules rather than writing the code. So here Pyforest solves the problem. Just add one module ‘import pyforest’ and we are ready to go.
Example 1:
Python3
# forget to import numpy # nut importing pyforest import pyforest # using numpy array = np.array([ 1 , 2 , 3 ]) print (array) print (active_imports()) |
Output:
[1 2 3] import numpy as np ['import numpy as np']
Example 2:
Python3
# forget to import numpy, matplotlib # but importing pyforest import pyforest # using numpy array = np.array([ 1 , 2 , 3 ]) # using matplotlib.pyplot plt.plot(array) print (active_imports()) |
Output:
Lazy import is a very useful feature of the Pyforest library as this feature automatically imports the library for us, if we don’t use the library it won’t be added. This feature is very useful to those who don’t want to write the import statements again and again in their code. The main benefit of this library is that it contains most of the libraries, and we can use the library as we like anytime without importing that library as the lazy import feature will do it for us.
Advantages of lazy_import() :
- It resolved the problem of importing libraries in the code snippet.
- It automatically sets the variable name as per the usage to avoid any error for libraries.
- Libraries will only be imported when you use them, no extra libraries will be added to reduce compilation time.
- It contains nearly 99% of the available Python libraries, which makes it easier to use as no extra library is needed.
- Its implementation is easy and the rest is done automatically
Lazy import in Python
In this article, we will learn how to do Lazy import in Python. For this, let’s first understand the lazy import.
Contact Us