How I can dynamically import Python module?


Python has a capability that allows you to build and store classes and functions for later usage. A module is the name of the file that has these collections of methods and classes. A module may contain more modules.

Let’s consider the following example to import multiple modules at once −

import sys, os, math, datetime 
print ('The modules are imported')

This imports all four of the following modules at once: datetime(to manipulate dates as date objects), math(consists of functions that can calculate different trigonometric ratios for a given angle), sys (for regular expressions), and os (for operating system features like directory listings).

The modules are imported

Importing modules dynamically

A computer program can load a library (or other binary) into memory at runtime, retrieve the addresses of the functions and variables it contains, call those functions or access those variables, and then unload the library from memory. This process is known as dynamic loading.

Following are the different ways to import modules dynamically.

Using __import__() method

All classes own the dunder method (also known as a magic method) __import__(), which starts and ends with a double underscore. It is utilised to import a module or class inside of a class instance.

Example

Similar results can be obtained by using the built-in __import__ function instead of the import statement, although this method actually accepts a string as an input. As if you had specified import sys, the variable sys is now the sys module. The os module is now the variable os, and so on.

math = __import__('math') os = __import__('os') sys = __import__('sys') datetime = __import__('datetime')

Output

As a result, __import__ imports a module but requires a string argument. A variable or the output of a function call may have been used in place of the hard-coded text that served as the module you imported in this instance.

Additionally, the variable to which you assign the module does not have to have the same name as the module. A list could be created by importing a number of modules.

>>> math
<module 'math' (built-in)>
>>> sys
<module 'sys' (built-in)>
>>> os
<module 'os' from 'C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\os.py'>
>>> datetime
<module 'datetime' from 'C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\datetime.py'>

Using imp module

The Python import mechanism, which loads code from packages and modules, is partially exposed by functions in the imp module. It is one method for dynamically importing modules and is helpful in situations where you are writing code but are unsure of the name of the module you need to import.

Example

Firstly, create a module ‘module1.py’ which contains a class called ‘Dynamic’. This module will be called when import_module() function is executed.

class Dynamic:
   def dynamic():
      print("Welcome to TutorialsPoint")

The use of the imp module is demonstrated in the example below. It provides the find_module() function which finds the module defined above in the current working directory. The import_module() function will dynamically import the module and its members in the program. The return statement then returns the module name and the name of the class in the module.

import imp import sys # function to dynamically load module def dynamic_module_import(module_name, class_name): # find_module() is used to find the module in current directory # it gets the pointer, path and description of the module try: file_pointer, file_path, description = imp.find_module(module_name) except ImportError: print ("Imported module {} not found".format(module_name)) try: # load_module dynamically loads the module # the parameters are pointer, path and description of the module load_module = imp.load_module(module_name, file_pointer, file_path, description) except Exception as e: print(e) try: load_class = imp.load_module("{}.{}".format(module_name, class_name), file_pointer, file_path, description) except Exception as e: print(e) return load_module, load_class if __name__ == "__main__": module, module_1 = dynamic_module_import("module1", "Dynamic") print (module,module_1)

Output

Following is an output of the above code −

<module 'module1' from 'C:\Users\Lenovo\Desktop\module1.py'> <module 'module1.Dynamic' from 'C:\Users\Lenovo\Desktop\module1.py'>

Using import_module from importlib package

The import module(moduleName) method from the importlib package can be used to dynamically import Python modules. You must provide a string representing moduleName.

Example

Following is an example to dynamically import module using importlib package −

from importlib import import_module moduleName = "os" globals()[moduleName] = import_module(moduleName) print ('The module name is :',moduleName)

Output

Following is an output of the above code −

The module name is : os

Dynamic imports using for loop

You can also do this from a for loop if you wish to dynamically import a list of modules.

Example

import importlib modnames = ["os", "sys", "math"] for lib in modnames: globals()[lib] = importlib.import_module(lib) print ('The module names are :',lib)

Output

The module names are : os
The module names are : sys
The module names are : math

A dict is the result of the globals() method. The object that is returned to us upon the import of a module can be set as the lib key for each library.

Updated on: 23-Nov-2022

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements