Modules
Import Aliases
A module alias keeps the namespace but changes the local name. Here stats is shorter, but readers can still see that mean belongs to the statistics module.
Source
import statistics as stats
scores = [8, 10, 9]
print(stats.mean(scores))
print(stats.__name__)Output
9
statisticsA name imported with from can also be aliased. Use this when the local name explains the role better than the original name.
Source
from math import sqrt as square_root
print(square_root(81))
print(square_root.__name__)Output
9.0
sqrtNotes
import module as aliaskeeps module-style access under a shorter or clearer name.from module import name as aliasimports one name under a local alias.- Prefer plain imports unless an alias improves clarity or follows a strong convention.
- Avoid
from module import *because it makes dependencies harder to see.
See also
Run the complete example
Expected output
9
statistics
9.0
sqrt
Execution time appears here after you run the example.