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 Program to Find the Size (Resolution) of a Image
Python Program to Find Armstrong Number in an Interval
Python hash()
Python String isupper()
Python Closures
Python Input, Output and Import
Python List append()
Python Program to Check If a List is Empty
Python Program to Create a Countdown Timer
Python Program to Find the Factors of a Number
Python Program to Capitalize the First Character of a String
Python Program to Trim Whitespace From a String
Python Program to Calculate the Area of a Triangle
Python Program to Get the Full Path of the Current Working Directory
Python Tuple count()
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python ascii()
Python dir()
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python Dictionary copy()
Python Program to Iterate Over Dictionaries Using for Loop
Python String ljust()
Deep Learning with Python - Francois Cholletf
Python String partition()
Python String isalpha()
Python Matrices and NumPy Arrays
Python Dictionary pop()
Python Program to Get File Creation and Modification Date
Python filter()
Python Program to Make a Flattened List from Nested List
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python object()