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 Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python File I/O Operation
Python Dictionary
Python property()
Python Custom Exceptions
Python Program to Safely Create a Nested Directory
Python Dictionary clear()
Python *args and **kwargs
APIs in Node.js vs Python - A Comparison
Python Program to Randomly Select an Element From the List
Python Set union()
Python Operator Overloading
Python Deep Learning Cookbook - Indra den Bakker
Python reversed()
Python List
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python Program to Append to a File
Python zip()
Python Program to Find the Largest Among Three Numbers
Python break and continue
Python if...else Statement
Python String translate()
Python String rjust()
Python Program to Check Prime Number
Python input()
Python Program to Shuffle Deck of Cards
How to get current date and time in Python?
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python Program to Find the Factors of a Number
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python Functions
Python issubclass()