Importlib package in Python
Last Updated :
23 Sep, 2022
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.
Important Functions
- importlib.import_module(name, package=None): The name of the module to be imported is passed as the first parameter to this function. In case the name of the module is to be specified in relative terms, the package parameter can also be used. It returns a module object that can be bound to any variable. Since Python 3.3, parent packages are automatically imported. If the module cannot be imported, import_module() raises ImportError.
- importlib.find_loader(name, path=None): This function finds the loader for the module to load it into the memory. Optionally, the user can also specify the path from where the module is to be loaded. It is deprecated since Python 3.4 and importlib.util.find_spec() is used instead.
- importlib.invalidate_caches(): It invalidates the internal caches that have been generated so that the module finder may implement changes in it that occur while the application is running.The internal caches of finders are stored at sys.meta_path.
- importlib.reload(module) : This function reloads a module that has been previously imported by the program. The module-level code because of this function is recompiled and re-executed. Since Python 3.7, when the module that is being reloaded lacks a ModuleSpec, a ModuleNotFoundError is raised.
- importlib.abc : This module contains all of the core abstract base classes used by import statements. The classes in this module are Finder (deprecated), MetaPathFinder, PathEntryFinder, and Loader.
- importlib.resources : This module allows Python’s import system to access resources within packages. A resource is any readable object contained in a package. It can access resources such as text as well as binary. The resources may include static files (such as templates, sample data, and certificates) as well as zip files.
- importlib.machinery : The various objects used to import and load modules such as SUFFIXES, Importer, PathFinder, etc are contained in this module.
- importlib.util : The code in this module can be utilized for importers. It includes the various objects needed to build an importer such as find_spec, module_from_spec, module_for_loader, spec_from_loader, LazyLoader, etc.
Note: The difference between import_module() & __import__ is that the former returns the specified module (Eg: two.one) only and the latter returns the top-level module or package (Eg: two).
Create two custom modules to be used in further programs.
Module 1:
Python3
def main():
print ( 'First module is imported!' )
return
if __name__ = = '__main__' :
main()
|
Module 2:
Python3
def main():
print ( 'Second module is imported!' )
return
if __name__ = = '__main__' :
main()
|
Note: All these programs should be in the same folder that we are going to make in this article.
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
import importlib
if __name__ = = '__main__' :
mod = importlib.import_module( 'mod1' )
print (mod.__name__)
mod.main()
mod = importlib.import_module( 'mod2' )
print (mod.__name__)
mod.main()
|
Output:
Example 2: Taking module name as input from the user
In this example, we are going to take the name of the module as input from the user and print the module name, documentation, and directory which includes all the functions and methods names.
Python3
import importlib
def dynamic_import(module):
return importlib.import_module(module)
if __name__ = = '__main__' :
custom = input ( "Enter module name: " )
module = dynamic_import(custom)
print (module.__name__)
print (module.__doc__)
print ( dir (module))
|
Output:
Example 3: Program for invalidate_caches & reload()
In this example, we are going to invalidate any previous cache stored and then reload the imported module, and at last check the before and after reloading the module that any changes in the module are done or not. as we can see in the output it returns true because there are no changes done in the module.
Python3
import importlib
importlib.invalidate_caches()
mod = importlib.import_module( 'mod1' )
print (mod)
remod = importlib. reload (mod)
print ( "Is reloaded module same as original? " ,
mod is remod)
|
Output:
Example 4: Fetch data from a text file using importlib.resource
In this example, we are going to fetch data from a text file using the importlib.resource in file handling and then print the data from that file. For that create a folder “texts” with __init__.py and sample.txt file in it where __init__.py is empty and samle.txt have some text. The resource will be read using functions such as open_text.
Python3
from importlib import resources
with resources.open_text( "texts" , "sample.txt" ) as t:
txt = t.readlines()
print ("".join(txt[: 7 ]))
|
As we see in the output text of the sample.txt file is printed.
Output:
Example 5: Printing the loader and path of the module
In this example, we are going to print the loader and path of the module using util.find_spec() and loader.load_module() methods of importlib modules.
Python3
import importlib
path = importlib.util.find_spec( 'mod1' )
print ( "Loader: " , path.loader)
m = path.loader.load_module()
print ( "Module: " , m)
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...