问题:您可以在Python中为导入的模块定义别名吗?

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

例如:

import a_ridiculously_long_module_name

…因此具有别名“ short_name”。

In Python, is it possible to define an alias for an imported module?

For instance:

import a_ridiculously_long_module_name

…so that is has an alias of ‘short_name’.


回答 0

import a_ridiculously_long_module_name as short_name

也适用于

import module.submodule.subsubmodule as short_name
import a_ridiculously_long_module_name as short_name

also works for

import module.submodule.subsubmodule as short_name

回答 1

在这里检查

import module as name

要么

from relative_module import identifier as name

Check here

import module as name

or

from relative_module import identifier as name

回答 2

如果已经完成:

import long_module_name

您还可以通过以下方式为其命名:

lmn = long_module_name

没有理由在代码中以这种方式执行此操作,但有时我发现它在交互式解释器中很有用。

If you’ve done:

import long_module_name

you can also give it an alias by:

lmn = long_module_name

There’s no reason to do it this way in code, but I sometimes find it useful in the interactive interpreter.


回答 3

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

import math as ilovemaths # here math module is imported under an alias name
print(ilovemaths.sqrt(4))  # Using the sqrt() function

Yes, modules can be imported under an alias name. using as keyword. See

import math as ilovemaths # here math module is imported under an alias name
print(ilovemaths.sqrt(4))  # Using the sqrt() function

回答 4

从MODULE导入TAGNAME作为ALIAS

from MODULE import TAGNAME as ALIAS


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。