您可以定义别名在Python导入的模块?
-
22-08-2019 - |
题
在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 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
不隶属于 StackOverflow