NameError in Python — What It Means and How to Fix It

What Python's NameError exception means and how to resolve it.

Published October 27, 2026

NameError is raised when a variable or function name is used before it's defined.

Key points

  • Catch it specifically with "except NameError:" when you want to handle this exact case rather than a bare except

Example

print(undefined_var)  # NameError: name 'undefined_var' is not defined
More Python articles