Can you define aliases for imported modules in Python?

PythonModuleAliasPython Import

Python Problem Overview


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

Python Solutions


Solution 1 - Python

import a_ridiculously_long_module_name as short_name

also works for

import module.submodule.subsubmodule as short_name

Solution 2 - Python

Check here

import module as name

or

from relative_module import identifier as name

Solution 3 - Python

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.

Solution 4 - Python

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

Solution 5 - Python

from MODULE import TAGNAME as ALIAS

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJordan ParmerView Question on Stackoverflow
Solution 1 - PythonvartecView Answer on Stackoverflow
Solution 2 - PythonBrian R. BondyView Answer on Stackoverflow
Solution 3 - PythonJohn FouhyView Answer on Stackoverflow
Solution 4 - PythonrealmanusharmaView Answer on Stackoverflow
Solution 5 - Python邢烽朔View Answer on Stackoverflow