__import__() in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

The __import()__ in the python module helps in getting the code present in another module by either importing the function or code or file using the __import__() in the python method. Whenever we need a specific module while executing the code, we can use this __import__() in the python module and import the libraries we want. Here we use the __import__() function to call the import statement.

Syntax of __import__ function in Python

The syntax of __import__() in the python method is given by:

where name is the module that we want to import for our use.

Parameters of __import__ function in Python

The __import__() in the python method has the following parameters, which are given at the import method's time.

  • name - This is the name of the library where the code present that we want to import or use is specified.
  • globals - By determining global, we specify how the name should be interpreted.
  • locals - By determining locals, we specify how the name must be interpreted.
  • fromlist - These are the submodules or the objects that must be specified to import them by name, mostly as a list.
  • level - This determines if the module that we are about to import should be absolute or relative imports. The default import is -1(absolute and relative).

Return Values of __import__ function in Python

The import in python returns the object or module that we specified while using the import module.

Exceptions of __import__ function in Python

We cannot say this as an exception, but we need to know that the use of the __import__() function in python is rarely used and not an everyday practice while executing code.

This is so because when we use the __import__() function, we can alter the semantics of the specified import statement, which is not a good practice and is highly discouraged too.

As we understood above, to call the import statement, we make use of the __import__() function, and this function holds the capability to alter the semantics of the import statement, which happens as this statement calls this function.

To avoid this, it is recommended to import hooks instead. Also we can also make use of importlib.import_module() for importing the module by name.

Example of __ import __() function in Python

Let us understand by the below example.

Code:

Output:

Explanation:

In the above example, we see how we can use the __import__() function in python. We also saw how the use of the __import__() function is discouraged as we are able to access the fabs() function by creating a new module which can lead to confusion sometimes. As shown above, we are able to access fabs in two ways: one is its original math module and the other as we imported it as maths_1.

This is discouraged as a change of semantics can lead to confusion. The fabs() function in python comes under the math module where we can get the absolute value of the number as a float.

What is the __ import __() Function in Python?

We can describe the import in python function as a module that contains the pre-defined codes that can be called via the import statement, which decreases the redundancy of code reuse. We mostly use the __import__() in python module for getting the code present that is present in another module by either importing the code or calling the function or file using the import__() in the python method.

We studied the syntax and its parameters, where describing the level is very important as it specifies which import type to choose. We have two import levels - Absolute and Relative.

  • Absolute Import - An absolute import determines that the import of the resources must be using the full path from the project’s root folder.
  • Relative Import - The relative import determines the import of the resources to happen from the current location, which is the location of the execution of the import statement happened.

We make use of the import in python in the rare case, but when we are executing the code and require a certain module during runtime, this import in the python function comes really handy.

How to use __ import __() in python?

We can use import by simply using its syntax (descriptive use can be seen in the following examples) as below:

We need to keep in mind to specify the parameters accordingly. In rare cases we use the __import__() in python and regular use of its is highly discouraged as well. This is so because it holds the capability to change the semantics of the import statement which happens as this statement calls this function.

More Examples

Example 1: To import NumPy Module

Lets us make the use of__import__() in python to import the numpy module as below.

Code:

Output:

Explanation:

As seen from the above code, we saw how we can import the module from another module and execute the code. We simply imported numpy as an array by specifying the relevant parameters. This is similar to using import numpy as np.

Conclusion.

  • The __import__() in python module helps in getting the code present in another module by either importing the function or code or file using the import in python method.
  • The import in python returns the object or module that we specified while using the import module.
  • The syntax of __import__() in python method is given by:
  • where,
    • name - Name of the library we want to import.
    • globals and locals - interpret the name.
    • fromlist - The submodules or objects that are required to import as a list
    • level - Determines whether to import as absolute or relative imports, default being -1(absolute and relative)
  • The use of the __import__() function in python is not an everyday practice and is rarely used. We discourage the use of the __import__() function in python as it holds the capability to alter the semantics of the import statement which happens as this statement calls this function.