Modules

Import Aliases

as gives imported modules or names a local alias.

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
statistics
import numpy as npnpnumpy module
`import x as y` binds the name y to the same module object x would have.

A 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
sqrt

Notes

See also

Run the complete example

Example code

Expected output

9
statistics
9.0
sqrt

Execution time appears here after you run the example.