Skip to main content

How to Give a Module a Shorter Name in Python with import as

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Some module names are long, and code uses them many times. import <module> as <name> gives a module a shorter name for your file, the way import pandas as pd does. This guide imports statistics as stats on Python 3.15.2.

Syntax​

Syntax
import <module> as <short name>
from <module> import <name> as <other name>
PartWhat it means
<module>The module to import, such as statistics or skops.io
as <short name>The name you'll use for it in this file
from <module> import <name> as <other name>The same, for one name from inside a module

The Python language reference says: "If the module name is followed by as, then the name following as is bound directly to the imported module."

The Example​

The script imports the standard statistics module as stats and works out the mean of three numbers.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

import_as.py
import statistics as stats

print(stats.mean([3, 5, 10]))

The import_as.py script in a macOS-style code window: import statistics as stats, then print(stats.mean([3, 5, 10])).

Step 2: Run It​

Terminal
uv run python import_as.py
Output
6

The terminal after uv run python import_as.py: 6.

stats.mean is statistics.mean under its new name: the mean of 3, 5 and 10 is 6. The command works unchanged in PowerShell.

One Name, Renamed​

The from form renames a single function. from statistics import mean as average, then print(average([3, 5, 10])), also printed 6.

Common Mistakes​

MistakeWhat happensDo this instead
Using the module's full name after renaming itNameError: name 'statistics' is not defined. Did you forget to import 'statistics'?Use the new name, stats, or import the module without as too
Inventing a different short name in every fileReaders have to look each one upUse the names everyone knows, such as pd for pandas and np for NumPy

Troubleshooting​

SymptomLikely causeFix
NameError for a module you importedIt was imported with as, so only the new name existsUse the name after as

FAQ​

Does as change the module for other files?​

No. The new name exists only in the file that imports it.

Can a module with dots in its name be renamed?​

Yes: import skops.io as sio gives the skops.io module the name sio.

Conclusion​

import statistics as stats made stats.mean([3, 5, 10]) work and print 6, and the full name statistics was no longer defined in that file. Use the short names most Python code already uses.

Additional Resources​