In Python, is it possible to define an alias for an imported module?
…so that is has an alias of ‘short_name’.
import a_ridiculously_long_module_name as short_name
also works for
import module.submodule.subsubmodule as short_name
import module as name
from relative_module import identifier as name
If you’ve done:
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.
Yes, modules can be imported under an alias name.
using as keyword.
import math as ilovemaths # here math module is imported under an alias name print(ilovemaths.sqrt(4)) # Using the sqrt() function
from MODULE import TAGNAME as ALIAS
Yes, you can define aliases for imported modules in Python.
Using pandas is considered a best practice in python because Pandas can import most file formats and link to databases.
Import pandas library
import pandas as pd
pd: is the conventional alias for pandas.
NP: is the conventional alias for Numpy.
Using short alias helps keep code (concise) and (clean).