Saturday, December 28, 2024
Google search engine
HomeLanguagesImportlib package in Python

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.

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




# Module1
# Test Function
  
def main():
    print('First module is imported!')
    return
  
if __name__ == '__main__':
    main()


Module 2:

Python3




# Module2
# Test Function
  
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




# 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 - Python

 

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




# Importing the package
import importlib
  
# Function to import module at the runtime
def dynamic_import(module):
    return importlib.import_module(module)
  
if __name__ == '__main__':
  
    # Storing input module name into a variable
    custom = input("Enter module name: ")
  
    # Calling function to import the 
    # module & store module object
    module = dynamic_import(custom)
      
    '''
    You can also use the default __import__ function as
    module = __import__(custom)
    '''
    # Printing name, documentation and 
    # directory list of the imported module
    print(module.__name__)
    print(module.__doc__)
    print(dir(module))


Output:

Importlib package - Python

 

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




# Importing the package
import importlib
  
# Invalidates any cache storage of module. 
# Any changes in the module will be reflected 
# and previous module storage will be invalidated.
importlib.invalidate_caches()
  
# Importing the module
mod = importlib.import_module('mod1')
print(mod)
  
# Reloading the module
remod = importlib.reload(mod)
print("Is reloaded module same as original? ",
      mod is remod)


Output:

Importlib package - Python

 

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.

Importlib package - Python

 

Python3




#Importing the module
from importlib import resources
  
# Using the open_text method to open and
# read the resource in the folder
with resources.open_text("texts", "sample.txt") as t:
    txt = t.readlines()
  
# Printing the contents of file
print("".join(txt[:7]))


As we see in the output text of the sample.txt file is printed.

Output: 

Importlib package - Python

 

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




# Importing package
import importlib
  
# find_spec: An abstract method for
# finding a path for the specified module.
path = importlib.util.find_spec('mod1')
print("Loader: ", path.loader)
  
# load_module is a legacy method for
# loading a module using util module.
# If the module cannot be loaded,
# ImportError is raised,
# otherwise the loaded module is returned.
m = path.loader.load_module()
print("Module: ", m)


Output:

Importlib package - Python

 

RELATED ARTICLES

Most Popular

Recent Comments