Python __import__() Function

Python 内置函数Python Built-in Functions


Description

__import__()This function is used to dynamically load classes and functions.

If a module changes frequently, you can use __import__() to dynamically load it.

Syntax

__import__ syntax:

__import__(name[, globals[, locals[, fromlist[, level]]]])

Parameter description:

  • name -- Module name

Return Value

Returns a tuple list.

Examples

The following example demonstrates how to use __import__:

a.py file code:

#!/usr/bin/env python #encoding: utf-8 import os print ('In the a.py file %s' % id(os))

test.py file code:

#!/usr/bin/env python #encoding: utf-8 import sys __import__('a') # Import the a.py module

Execute the test.py file, and the output is:

在 a.py 文件中 4394716136

Python 内置函数Python Built-in Functions

Other Extensions