Python __import__()

Python __import__() builtin function is invoked by the import statement. We may replace the default behaviour of import by importing builtins and assigning a new __import__() function call to builtins.__import__.

Please note that it is advised to use import statement instead of __import__() function.

In this tutorial, we will learn about the syntax of Python __import__() function, and learn how to use this function with the help of examples.


The syntax of __import__() function is

import(name, globals=None, locals=None, fromlist=(), level=0)


nameRequiredA string. Name of the module to import.
globalsOptionalA dictionary. Default value is None.
localsOptionalA dictionary. Default value is None.
fromlistOptionalA list of strings. Names of the objects or submodules that should be imported from the module. Default value is ().
levelOptionalAn integer. Specifies whether to use absolute or relative imports. Default value is 0.


In this example, we import math module using __import__() builtin function.

Python Program

math_module = __import__('math', globals(), locals(), [], 0)
Try Online




In this Python Tutorial, we have learnt the syntax of Python __import__() builtin function, and also learned how to use this function, with the help of Python example programs.