The __import__ python is an in-built function present in the Python library. When we create programs in Python, we may need different modules to use different functions and methods that are in-built in Python. However, while writing an extensive number of lines of codes, it can get difficult to plan all the modules that will need to be included in the program.
Thus, when one needs to import a module in the middle of the runtime of the code, then that module can be imported using the __import__ () function. The __import__ () function is called by the import statement to import any required module during the runtime of the code.
__import__ () Parameters
The syntax that is followed to use the __import__ () function in a Python code is provided below.
__import__ (name, globals = None, locals = None, fromlist = (), level = 0)
In the syntax above, one can notice that five parameters need to be passed to implement the __import__ () Python function in the code. Here, the parameter name represents the name of the module that needs to be imported. After this, globals and locals are mentioned as parameters. These determine how the program will interpret the module that is being imported.
The next parameter is fromlist. It defines the objects or submodules present in the called module that need to be imported into the program. These objects are imported in the form of a list. Last is the level that specifies whether absolute or relative import will be used.
Use of __import__ () is Discouraged
The aim of the __import__ () function is to import a module that needs to be used for only a few lines of the code in the program. It can also be used to create a copy of an object from the module and then modify to use it anywhere in the program.
The __import__ () may seem beneficial but in the everyday program, one seldom comes across such a situation when using the __import__ () function is unavoidable. The direct use of this function is rare. Mostly, the use of this function is discouraged.
One can change the semantics of the import statement using the __import__ () function as the statement calls the function. However, when one needs to import any module by its name, then the function importlib.import_module () can be used. It is a much more preferable alternative to the __import__ () function.
Example: How __import__ () works?
var = __import__ (‘numpy’, globals (), locals (), , 0)
a = var.array ([1, 2, 3])
print (type (a))
The program will provide the output that displays the name of the module imported and the name of the object for which the module is used.
The program here uses the array method that is defined in the module names numpy. The array () method is used to create and manage a group of the same type of data in the form of an arranged sequence called an array.
maths = __import__ (‘math’, globals (), locals (), , 0)
print (maths.fabs (-3.7))
Here, the fabs () method is defined in the math module. In a usual case when __import__ () function is not used, the module will be imported in the following manner.
Both the methods provide the same result.
Frequently Asked Questions:
Q1. What is the __import__ () in Python?
The __import__ () Python function is an in-built function provided through which any module can be called anywhere in the runtime of the program. The attributes and specifications for the module are mentioned as the parameters of the __import__ () function.
Q2. What is __all__ in Python?
The __all__ is a variable in Python that provides the list of all the public objects of a particular module. It specifies all the modules that can be imported into the namespace in current use.
Q3. What is from __future__ import?
The from __future__ is a module in Python that ensures that the existing tools are accurately used to analyse the import statements. It is used to check what kind of features can be imported to the current Python interpreter.
Q4. How do I fix an import error in Python?
To fix the import error, the script called run.py will need to be added and edited.