Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you define aliases for imported modules in Python?

People also ask

Can we create an alias for module *?

If you want to refer to a module by using a different name, you can create an alias.

What happens when a Python module is imported?

When a module is first imported, Python searches for the module and if found, it creates a module object 1, initializing it. If the named module cannot be found, a ModuleNotFoundError is raised. Python implements various strategies to search for the named module when the import machinery is invoked.

What is module aliasing?

Having multiple version of a Python package/module/file is very common. Manipulating PYTHONPATH or using virtualenvs are a way to use various versions without changing your code.

What is an alias in Python?

In python programming, the second name given to a piece of data is known as an alias. Aliasing happens when the value of one variable is assigned to another variable because variables are just names that store references to actual value.


import a_ridiculously_long_module_name as short_name

also works for

import module.submodule.subsubmodule as short_name

Check here

import module as name

or

from relative_module import identifier as name

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.