as shows up in several unrelated Python constructs, always meaning 'bind this to a different, local name': import module as alias lets you refer to a module by a shorter or conflict-free name, like the widespread convention of aliasing numpy to np; from module import name as alias renames a specific imported name; except ExceptionType as e binds the caught exception object to e; and with expr as name binds whatever a context manager's __enter__ returns. In every case, as is just introducing a local binding for something on its left.
1Understanding as Keyword
as shows up in several unrelated Python constructs, always meaning 'bind this to a different, local name': import module as alias lets you refer to a module by a shorter or conflict-free name, like the widespread convention of aliasing numpy to np; from module import name as alias renames a specific imported name; except ExceptionType as e binds the caught exception object to e; and with expr as name binds whatever a context manager's __enter__ returns. In every case, as is just introducing a local binding for something on its left.
Follow established community aliasing conventions, like aliasing numpy to np or pandas to pd, rather than inventing your own — consistent aliases make code instantly recognizable to anyone familiar with those libraries.
import datetime as dt
now = dt.datetime(2026, 1, 1)
print(now.year)2Practical Example
Here is a real-world application of as Keyword showing how it is used in production Python code.
try:
import numpy as np
except ImportError as e:
print(f"numpy not available: {e}")
np = None3Best Practices
Follow these guidelines when working with as Keyword:
1. Use as to shorten long or commonly-typed module names, following widely recognized conventions where they exist
2. Use as to avoid a naming collision when two modules would otherwise export the same name
3. Give the exception variable in an except clause a short, conventional name like e, since it's typically used briefly right where it's caught
Tip: Follow established community aliasing conventions, like aliasing numpy to np or pandas to pd, rather than inventing your own — consistent aliases make code instantly recognizable to anyone familiar with those libraries.
import datetime as dt
now = dt.datetime(2026, 1, 1)
print(now.year)