在 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
从模块导入 TAGNAME 作为别名
是的,您可以在 Python 中为导入的模块定义别名。
在 python 中使用熊猫被认为是 最佳实践,因为熊猫可以导入大多数文件格式并链接到数据库。
导入熊猫图书馆
import pandas as pd
解释:
Pd : 是 熊猫的常规别名。
NP : 是 麻木的常规别名。
使用短别名有助于保持代码 (简明)和 (干净)。