Skip to content

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.

Beginner 6 min read·Lesson 213 of 306 in Python Programming

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

Python
import statistics as stats
from datetime import datetime as dt
print(stats.median([3, 1, 2]))
print(dt(2026, 11, 27))
  • as renames the import locally.
  • Follow community conventions.
  • Use aliases to avoid clashes.

Code examples

A module alias

Python
import statistics as stats
print(stats.median([3, 1, 2]))
Output
2

Name aliases

Python
from datetime import datetime as dt, timedelta as td
print(dt(2026, 11, 27) + td(days=5))
Output
2026-12-02 00:00:00

More examples

Resolving a clash

Python
from pathlib import Path
import importlib
Path("metric.py").write_text('''def convert(km):
return f"{km * 1000} m"
''', encoding="utf-8")
Path("imperial.py").write_text('''def convert(km):
return f"{km * 0.621371:.2f} miles"
''', encoding="utf-8")
importlib.invalidate_caches()
from metric import convert as to_metres
from imperial import convert as to_miles
print(to_metres(5), "|", to_miles(5))
print(to_metres.__module__, to_miles.__module__)
Output
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:

TextOutput
5000 m | 3.11 miles
metric imperial

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

import collections as c
import json as j

Prefer

import collections
import json

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

  1. 1
    Import xml.etree.ElementTree with the conventional alias ET and parse a tiny XML string.
  2. 2
    Create 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?
Practise this topic