在Python中,是有可能定义一个别名已导入模块?

例如:

import a_ridiculously_long_module_name

...所以即具有 'SHORT_NAME' 的别名。

有帮助吗?

解决方案

import a_ridiculously_long_module_name as short_name

也适用于

import module.submodule.subsubmodule as short_name

其他提示

检查这里

import module as name

from relative_module import identifier as name

如果你这样做:

import long_module_name

也可以通过给它一个别名:

lmn = long_module_name

有没有理由做这样的代码,但我有时发现在交互式解释是有用的。

是,模块可以被下一个别名导入。 使用的作为关键字。 参见

import math as ilovemaths # here math module is imported under an alias name
print(ilovemaths.sqrt(4))  # Using the sqrt() function
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top