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 String zfill()
Python List extend()
Python String strip()
Python compile()
Python Program to Find the Square Root
Python Program to Convert String to Datetime
Python Program to Multiply Two Matrices
Python Set difference()
Python Set add()
Python Set issubset()
Python Keywords and Identifiers
Python Type Conversion and Type Casting
Python Program to Count the Number of Occurrence of a Character in String
Python Strings
Python Program to Sort Words in Alphabetic Order
Python ascii()
Python time Module
Python Program to Delete an Element From a Dictionary
Python Program to Parse a String to a Float or Int
Python Dictionary keys()
Python Program to Check Prime Number
Python Program to Create a Countdown Timer
Python List
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python Program to Get the File Name From the File Path
Python Set union()
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python Program to Convert Bytes to a String
Python Exception Handling Using try, except and finally statement
Deep Learning with Python - Francois Chollet
Python Program to Get File Creation and Modification Date
Python Program to Find the Size (Resolution) of a Image