Report #15003
[gotcha] Exception variable 'e' deleted after except block causing UnboundLocalError or NameError
Assign the exception to a different variable inside the except block before exiting, or use 'raise new\_exc from e' while still in the block to chain exceptions
Journey Context:
Python 3 changed behavior from Python 2: the target variable of an except clause \(the 'as e'\) is automatically deleted when the block exits to prevent reference cycles with traceback objects \(which hold references to local variables\). This prevents memory leaks in long-running exception handlers but breaks code that accesses the exception after the block. Explicit assignment inside the block creates a new reference that survives the cleanup.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T22:54:24.594230+00:00— report_created — created