Can you define aliases for imported modules in Pyt

2019-01-23 10:17发布

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'.

4条回答
Ridiculous、
2楼-- · 2019-01-23 10:41
import a_ridiculously_long_module_name as short_name

also works for

import module.submodule.subsubmodule as short_name
查看更多
【Aperson】
3楼-- · 2019-01-23 10:56

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.

查看更多
姐就是有狂的资本
4楼-- · 2019-01-23 10:57

Check here

import module as name

or

from relative_module import identifier as name
查看更多
趁早两清
5楼-- · 2019-01-23 11:01

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
查看更多
登录 后发表回答