Module 14 · Python Modules and Packages — topic 5 of 13
Aliases
Renaming imports with as: shortening long module names, community conventions such as np and pd, resolving clashes between functions with the same name, and when aliases hurt readability.
Learning objectives
- Create module and name aliases
- Use conventional aliases
- Resolve import name clashes with aliases
Definition
The as keyword gives an imported module or name a different local name: import statistics as stats, from json import loads as parse_json. The original module is unchanged; only the name in your file differs.
Introduction
Data scientists write np and pd thousands of times a day instead of numpy and pandas. Aliases make long names short and resolve conflicts between modules.
Why it matters
Aliases improve readability when used conventionally, and they are the cleanest fix when two modules export functions with the same name.
Detailed explanation
import module as short binds the module to short; the original name is not bound.
from module import name as other binds the object under a new name.
Some aliases are universal conventions: import numpy as np, import pandas as pd, import matplotlib.pyplot as plt. Follow them so others can read your code.
Aliases resolve clashes: from metric import convert as to_metres and from imperial import convert as to_miles.
Avoid inventing obscure aliases (import collections as c) — they make code harder to read and search.
Aliases don't affect the imported object: to_miles.__module__ still reports the original module.
Aliases also help when a module has been renamed or replaced: import a compatible alternative under the old name in one place (for example, import tomllib as toml_parser) and the rest of the code doesn't need to change.
Syntax
- as renames the import locally.
- Follow community conventions.
- Use aliases to avoid clashes.
Code examples
A module alias
2
Name aliases
2026-12-02 00:00:00
More examples
Resolving a clash
5000 m | 3.11 miles metric imperial
Step-by-step explanation
- Both modules define convert; without aliases the second import would replace the first.
- Descriptive aliases make each call's meaning clear.
- __module__ shows each function still knows where it was defined.
Expected output
Running “Resolving a clash” prints exactly:
Practical applications
- import numpy as np, import pandas as pd in data science.
- import xml.etree.ElementTree as ET for long module paths.
Best practices
- Use aliases for long names and established conventions.
- Choose descriptive aliases for clashes.
- Don't alias short module names.
Common mistakes
Cryptic aliases
Avoid
Prefer
Short modules don't need aliases; cryptic names hurt readability.
Important notes
- Aliases are only local names; other modules importing the same module are unaffected.
Key takeaways
- as gives an import a local name.
- Follow conventions like np and pd.
- Use aliases to resolve name clashes.
Try it yourself
- 1Import xml.etree.ElementTree with the conventional alias ET and parse a tiny XML string.
- 2Create two modules with a function called load and use both in one script via aliases.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- ModerateWhat is printed?