Remember
__import__() performs module importing. Ordinary import statements are usually clearer for normal application code.
__import__() performs module importing. Ordinary import statements are usually clearer for normal application code.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
module = __import__("math")
print(module.isqrt(9))Top-level scope
__import__() performs module importing. Ordinary import statements are usually clearer for normal application code.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
module = __import__("math")
print(module.isqrt(9))
|
v
DISPLAYED RESULT
3Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
module = __import__("math")
print(module.isqrt(9))Top-level scope
The example has finished. Compare the final output with the values and references above.
3
What do you expect this program to print?
module = __import__("math")
print(module.isqrt(9))__import__() FunctionThe __import__() function in Python is a special built-in function that is used to import modules dynamically at runtime. While you typically use the import keyword, this function is what Python uses behind the scenes to make that happen.
__import__(name, globals=None, locals=None, fromlist=(), level=0)
name – Required. The name of the module to import (as a string).globals – Optional. Usually globals().locals – Optional. Usually locals().fromlist – Optional. A list of names to import from the module.level – Optional. Used for relative imports (0 = absolute import).math_module = __import__('math')
print(math_module.sqrt(16))
4.0
If you want to import specific objects (like sqrt or pi) from a module, use the fromlist parameter.
mod = __import__('math', fromlist=['sqrt'])
print(mod.sqrt(25))
5.0
module_name = input("Enter module to import: ")
mod = __import__(module_name)
print(f"Successfully imported: {mod.__name__}")
import statement__import__() only when dynamic behavior is absolutely neededimport unnecessarily can make code harder to readImportError if the module doesn't existThis function is rarely used in interviews, but knowing it can show your depth in Python internals or dynamic programming techniques.
__import__() dynamically imports modules in PythonWrite a program that takes a module name as input and tries to import it using __import__(). If it fails, print an error message.
try:
name = input("Module name: ")
mod = __import__(name)
print(f"{name} imported successfully!")
except ImportError:
print("Module not found!")
Connect what you noticed in the example with the idea below.
__import__() performs module importing. Ordinary import statements are usually clearer for normal application code.
__import__() reads a variable from an unrelated function.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.