Example 1: Importing custom modules using importlib module.
In this example, we are importing the custom module that we have created above using the importlib module that custom module is imported in this program in the middle of execution, not at the time of initialization that we are imported normally using “import”. and we printed the name of the imported module using the “var_name.__name__” command.
Python3
# Importing module import importlib if __name__ = = '__main__' : # Importing the created module using the # import_module function into a variable mod = importlib.import_module( 'mod1' ) # Printing the name of module print (mod.__name__) # Calling the function from the imported module mod.main() # Importing the created module using the # import_module function into a variable mod = importlib.import_module( 'mod2' ) # Printing the name of module print (mod.__name__) # Calling the function from the imported # module mod.main() |
Output:
Importlib package in Python
In this article, we are going to know about the Importlib package in the Python programming language.
The importlib package is primarily utilized by Python applications for dynamic imports during runtime. In layman’s terms, it allows the user to load modules as he/she discovers them. This package has enabled programmatic module import since Python 3. This package also allows users to create and use custom objects (known as importers).
Note: Since Python 3.3, the import statement itself is implemented by importlib package, not by very complex C code.