Can you define aliases for imported modules in Python?

Question:

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

Asked By: Jordan Parmer

||

Answers:

import a_ridiculously_long_module_name as short_name

also works for

import module.submodule.subsubmodule as short_name
Answered By: vartec

Check here

import module as name

or

from relative_module import identifier as name
Answered By: Brian R. Bondy

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.

Answered By: John Fouhy

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
Answered By: realmanusharma

from MODULE import TAGNAME as ALIAS

Answered By: 邢烽朔

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.

Example:
Import pandas library

import pandas as pd

Explaining:

pd: is the conventional alias for pandas.

NP: is the conventional alias for Numpy.

Using short alias helps keep code (concise) and (clean).

Answered By: Osamah Elamin
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.