Table of Contents
The __import__() is a function that is called by the import statement.
The syntax of the __import__() function is:
__import__(name, globals=None, locals=None, fromlist=(), level=0)
1. __import__() Parameters
- name – the name of the module you want to import
- globals and locals – determines how to interpret name
- fromlist – objects or submodules that should be imported by name
- level – specifies whether to use absolute or relative imports
2. Use of __import__() is Discouraged
This __import__() function is not necessary for everyday Python program. It is rarely used and often discouraged.
This function can be used to change the semantics of the import statement as the statement calls this function. Instead, it is better to use import hooks.
And, if you want to import a module by name, use importlib.import_module().
3. Example: How __import()__ works?
mathematics = __import__('math', globals(), locals(), [], 0)
print(mathematics.fabs(-2.5))
Output
2.5
The fabs() method is defined in the math module. You can call this function using the following syntax:
import math math.fabs(x)
However, in the above program, we changed the way fabs() works. Now, we can also access fabs() using the following syntax:
mathematics.fabs(x)
Related posts:
Python bool()
Python Program to Find Factorial of Number Using Recursion
Python String swapcase()
Python Program to Find the Size (Resolution) of a Image
Python enumerate()
Python String isidentifier()
Python String rpartition()
Python Program to Slice Lists
Python Function Arguments
Python str()
Python Set symmetric_difference_update()
Python type()
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python hash()
Python Program to Get the Class Name of an Instance
Python frozenset()
Python Program to Multiply Two Matrices
Python Closures
Python Program to Convert Celsius To Fahrenheit
Python Program to Count the Occurrence of an Item in a List
Python Program to Append to a File
Python @property decorator
Python Set intersection_update()
Python vars()
Python String islower()
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python classmethod()
Python Program to Check if a Number is Positive, Negative or 0
Python Program to Convert Kilometers to Miles
Python Program to Check if a Number is Odd or Even
Python break and continue
Python Program to Find the Largest Among Three Numbers