Can you define aliases for imported modules in Python?

In Python, is it possible to define an alias for an imported module?

For instance:

import a_ridiculously_long_module_name that is has an alias of 'short_name'.

Asked by: Carlos918 | Posted: 06-12-2021

Answer 1

import a_ridiculously_long_module_name as short_name

also works for

import module.submodule.subsubmodule as short_name

Answered by: John228 | Posted: 07-01-2022

Answer 2

Check here

import module as name


from relative_module import identifier as name

Answered by: Joyce815 | Posted: 07-01-2022

Answer 3

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: Lenny452 | Posted: 07-01-2022

Answer 4

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: Leonardo917 | Posted: 07-01-2022

Answer 5

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


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: Blake598 | Posted: 07-01-2022

Answer 6


Answered by: Chester723 | Posted: 07-01-2022

Similar questions

import - What is the scope for imported classes in python?

Please excuse the vague title. If anyone has a suggestion, please let me know! Also please retag with more appropriate tags! The Problem I want to have an instance of an imported class be able to view things in the scope (globals, locals) of the importer. Since I'm not sure of the exact mechanism at work here, I can describe it much better with snippets than words. ## Fil...

python - Getting file path of imported module

This question already has answers here:

import - How to reload a Python module that was imported in another file?

I am trying to learn how Python reloads modules, but have hit a roadblock. Let's say I have: dir1\ from dir2.file2 import ClassOne myObject = ClassOne() dir1\dir2\ class ClassOne(): def reload_module(): reload(file2) The reload call fails to find module "file2". My question is, how do I ...

matlab - How to access fields in a struct imported from a .mat file using loadmat in Python?

Following this question which asks (and answers) how to read .mat files that were created in Matlab using Scipy, I want to know how to access the fields in the imported structs. I have a file in Matlab from which I can import a struct: >> load bla % imports a struct called G >> G G = Inp: [40x40x...

package - Find which python modules are being imported

What's an easy way of finding all the python modules from a particular package that are being used in an application?

python - How to prevent a module from being imported twice?

When writing python modules, is there a way to prevent it being imported twice by the client codes? Just like the c/c++ header files do: #ifndef XXX #define XXX ... #endif Thanks very much!

unit testing - Mocking imported modules in Python

I'm trying to implement unit tests for function that uses imported external objects. For example is: import os import pylons def some_func(arg): ... var1 = os.path.exist(...) var2 = os.path.getmtime(...) var3 = pylons.request.environ['HTTP_HOST'] ... So when I'm creating unit test for it I do some mocking (minimock in my case) and replaci...

python - Using imported modules in more than one file

This question is a bit dumb but I have to know it. Is there any way to use imported modules inside other imported modules? I mean, if I do this: -main file- import os import othermodule othermodule.a() -othermodule- def a(): return os.path.join('/', 'example') # Without reimporting the os module The os module is not recognized by the...

python - Using Pygame Module from imported script

Greetings! I'm creating a simple snake game. I want to expand my classes in different modules e.i. have menu class in a separate script from my main game loop. In other words, I want my imported script to take the pygame init which was called earlier in main script. Here is a quick example using pseudo code of my problem: def version(): print pygame.version In ma...

What could cause a python module to be imported twice?

As far as I understand, a python module is never imported twice, i.e. the code in the module only gets executed the first time it is imported. Subsequent import statements just add the module to the scope of the import. I have a module called "" that seems to be imported multiple times though. I use pdb to print the stack at the top of the code for this module. Here is the end of the stack tr...

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python