But what if the name of the required module is known to us only at runtime? How can we import this module? You can use the built-in
__import__() . It also helps to import modules at runtime.
Syntax: __import __ (name, globals, locals, fromlist, level)
name : Name of the module to be imported
globals and locals : Interpret names
formlist : Objects or submodules to be imported (as a list)
level : Specifies whether to use absolute or relative imports. Default is -1 (absolute and relative).
Example # 1 :
& lt; class `numpy.ndarray` & gt;
Example # 2 :
Both of the following statements have the same meaning and do the same job.
__import__() is not really required in day to day Python programming. Its direct use is rare. But sometimes, when you need to import modules at runtime, this feature is quite useful.