The Python Oracle

Can you define aliases for imported modules in Python?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Track title: CC C Schuberts Piano Sonata No 13 D

--

Chapters
00:00 Question
00:27 Accepted answer (Score 255)
00:42 Answer 2 (Score 51)
00:57 Answer 3 (Score 40)
01:18 Answer 4 (Score 3)
01:37 Thank you

--

Full question
https://stackoverflow.com/questions/7065...

Answer 1 links:
[Check here]: http://docs.python.org/reference/simple_...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #module #alias #pythonimport

#avk47



ACCEPTED ANSWER

Score 272


import a_ridiculously_long_module_name as short_name

also works for

import module.submodule.subsubmodule as short_name



ANSWER 2

Score 57


Check here

import module as name

or

from relative_module import identifier as name



ANSWER 3

Score 41


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.




ANSWER 4

Score 3


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