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 int()
Python if...else Statement
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python Program to Measure the Elapsed Time in Python
Python dir()
Python Program to Display Powers of 2 Using Anonymous Function
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python Program to Get the Last Element of the List
Python Program to Sort Words in Alphabetic Order
Python bool()
Python Program to Convert Bytes to a String
Python String isidentifier()
Python Program to Iterate Over Dictionaries Using for Loop
Deep Learning in Python - LazyProgrammer
Python String rjust()
Python Program to Find All File with .txt Extension Present Inside a Directory
Python datetime
Python String splitlines()
Python Program to Shuffle Deck of Cards
Python Global Keyword
Python Dictionary update()
Introduction to Scientific Programming with Python - Joakim Sundnes
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python List index()
Deep Learning with Python - Francois Cholletf
Python String isnumeric()
Intelligent Projects Using Python - Santanu Pattanayak
Python Program to Get the Full Path of the Current Working Directory
Python Program to Print Hello world!
Python Program to Create a Long Multiline String
Python set()
Python String rindex()