The Python Oracle

Can you define aliases for imported modules in Python?

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Techno Intrigue Looping

--

Chapters
00:00 Can You Define Aliases For Imported Modules In Python?
00:20 Accepted Answer Score 272
00:31 Answer 2 Score 57
00:42 Answer 3 Score 41
01:01 Answer 4 Score 3
01:13 Thank you

--

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

--

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