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 exec()
Python bin()
Python Program to Count the Number of Each Vowel
Python Program to Check if a Number is Odd or Even
How to Get Started With Python?
Python String casefold()
Python Set copy()
Python property()
Python Program to Generate a Random Number
Python next()
Python compile()
Python List append()
Python Program to Catch Multiple Exceptions in One Line
Python format()
Python Program to Measure the Elapsed Time in Python
Python Functions
Python dict()
Python String find()
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python frozenset()
Python callable()
Python Anonymous / Lambda Function
Python Program to Make a Flattened List from Nested List
Python Set intersection_update()
Python String startswith()
Python Program Read a File Line by Line Into a List
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python Program to Copy a File
Python eval()
Python Program to Convert Bytes to a String
Python Program to Print all Prime Numbers in an Interval
Deep Learning with Python - Francois Cholletf