161

在 Python 中,是否可以为导入的模块定义别名?

例如:

import a_ridiculously_long_module_name

...所以它有一个别名'short_name'。

4

5 回答 5

224
import a_ridiculously_long_module_name as short_name

也适用于

import module.submodule.subsubmodule as short_name
于 2009-04-01T17:34:00.300 回答
43

在这里检查

import module as name

或者

from relative_module import identifier as name
于 2009-04-01T17:34:03.450 回答
35

如果您已经完成:

import long_module_name

你也可以给它一个别名:

lmn = long_module_name

没有理由在代码中这样做,但我有时发现它在交互式解释器中很有用。

于 2009-04-01T22:22:14.257 回答
1

是的,可以使用别名导入模块。用作关键字。看

import math as ilovemaths # here math module is imported under an alias name
print(ilovemaths.sqrt(4))  # Using the sqrt() function
于 2018-10-17T17:07:51.853 回答
-4

从 MODULE 导入 TAGNAME 作为 ALIAS

于 2020-05-11T15:06:00.527 回答