Python内置函数(68)——__import__

英文文档

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

This function is invoked by the import statement. It can be replaced (by importing the builtins module and assigning to builtins.import) in order to change semantics of the import statement, but doing so is strongly discouraged as it is usually simpler to use import hooks (see PEP 302) to attain the same goals and does not cause issues with code which assumes the default import implementation is in use. Direct use of import() is also discouraged in favor of importlib.import_module().

The function imports the module name, potentially using the given globals and locals to determine how to interpret the name in a package context. The fromlist gives the names of objects or submodules that should be imported from the module given by name. The standard implementation does not use its locals argument at all, and uses its globals only to determine the package context of the import statement.

level specifies whether to use absolute or relative imports. 0 (the default) means only perform absolute imports. Positive values for level indicate the number of parent directories to search relative to the directory of the module calling import() (see PEP 328 for the details).

When the name variable is of the form package.module, normally, the top-level package (the name up till the first dot) is returned, not the module named by name. However, when a non-empty fromlist argument is given, the module named by name is returned.

说明

函数功能用于动态的导入模块,主要用于反射或者延迟加载模块。

import(module)相当于import module

先定义两个模块mian.py和index.py,两个文件在同一目录下:

1
2
3
4
5
6
7
8
9
#index.py
print ('index')
def sayHello():
print('hello index')
def sayHelloZhCn():
print('你好 index')
1
2
3
4
5
6
7
#mian.py
print ('main')
index = __import__('index')
dir(index)
index.sayHello()
index.sayHelloZhCn()

执行main.py,可以证实动态加载了index.py,import返回的模块也是index模块

1
2
3
4
5
C:\Users\Admin\Documents\Python3\importtest>python main.py
main
index
hello index
你好 index

import(package.module)相当于from package import name,如果fromlist不传入值,则返回package对应的模块,如果fromlist传入值,则返回package.module对应的模块。

先定义archives包,其中包含user和role两个模块:

1
2
3
4
5
#__index__.py
print ('archives.__index__')
def sayHello():
print('hello archives')

1
2
3
4
5
#user.py
print ('user')
def sayHello():
print('hello user')
1
2
3
4
5
#role.py
print ('role')
def sayHello():
print('hello role')

结构如下:
结构
修改mian.py:

1
2
3
4
5
6
#main.py
print ('main')
archives = __import__('archives')
archives.sayHello()
archives.user

执行main.py,可以证实动态加载了archives包,import返回的模块也是archives模块

1
2
3
4
5
6
7
8
C:\Users\Admin\Documents\Python3\importtest>python main.py
main
archives.__index__
hello archives
Traceback (most recent call last):
File "main.py", line 5, in <module>
archives.user
AttributeError: module 'archives' has no attribute 'user'

修改mian.py:

1
2
3
4
5
6
#main.py
print ('main')
archives = __import__('archives.user')
archives.sayHello()
print(archives.user)

执行main.py,可以证实动态加载了archives包的user模块,import返回的模块也是archives模块

1
2
3
4
5
6
7
C:\Users\Admin\Documents\Python3\importtest>python main.py
main
archives.__index__
user
hello archives
<module 'archives.user' from 'C:\\Users\\Admin\\Documents\\Python3\\import
test\\archives\\user.py'>

修改mian.py:

1
2
3
4
5
6
#main.py
print ('main')
archives = __import__('archives.user',fromlist = ('user',))
archives.sayHello()
print(archives)

执行main.py,可以证实动态加载了archives包的user模块,import返回的模块是user模块

1
2
3
4
5
6
7
C:\Users\Admin\Documents\Python3\importtest>python main.py
main
archives.__index__
user
hello user
<module 'archives.user' from 'C:\\Users\\Admin\\Documents\\Python3\\import
test\\archives\\user.py'>

level参数,指定是使用绝对导入还是相对导入。 0(默认值)表示只执行绝对导入。