liveBook · Manning

Import alias

As explained in Python Workout: 50 ten-minute exercises

A module alias is an alternate name bound to an imported module by an import statement using as.

Long module names can make repeated references awkward. An import statement using as binds the module to a shorter name: import mymod as mm. The original name mymod is not defined; mm is. section 8.0

Shortening a frequently used module name

import numpy as np # Refer to the module through np np section 8.0

A module alias changes the name available in your namespace, not the module’s contents. This differs from importing selected attributes, such as from os import sep, which defines sep rather than os. section 8.0

How Python Workout: 50 ten-minute exercises covers it

How deep it goes
Defines alias syntax and namespace behavior, but stops short of deeper import machinery.
Examples
Uses mymod as mm and numpy as np as concrete alias forms.
What it assumes
Assumes familiarity with importing modules and teaches the as form for binding alternate names.
What you can do after
Apply module aliases through Python import statements and use the bound name in subsequent references.
How it connects
Connects module aliases with modules, namespaces, attributes, and import statements.

Also explained in

Compare how 3 books explain import alias →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Workout: 50 ten-minute exercises. The section links point to the parts of the book it draws on.