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.

Similar Reads

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....

Example 1: Importing custom modules using importlib module.

...

Example 2: Taking module name as input from the user

...

Example 3: Program for invalidate_caches & reload()

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....

Example 4: Fetch data from a text file using importlib.resource

...

Example 5: Printing the loader and path of the module

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....