Post

Python __import__()

The __import__() is a built-in python function that is used to call the import statement.

Python __import__()

The syntax of the import() function is:

1
__import__(name, globals=None, locals=None, fromlist=(), level=0)

import() Parameters

The import() function takes multiple parameters as argument:

  • name - name of the module to import
  • globals and locals - interpret names
  • fromlist - Objects or submodules to be imported
  • level - specifies whether to use absolute or relative imports

Example 1: How to use import() function in python?

1
2
mathematics = __import__('math', globals(), locals(), [], 0)
print(mathematics.fabs(-2.5))

Output:

1
2.5
This post is licensed under CC BY 4.0 by the author.