Python Error

NameError

NameError is raised when Python encounters a name (variable, function, class) that hasn't been defined in the current scope. This includes typos, variables used before assignment, and missing imports.

Common causes

  • Using a variable before assigning it
  • Typo in variable or function name
  • Forgetting to import a module or name
  • Variable defined inside a function but used outside
  • Using a Python 2 name in Python 3 (e.g., print without parentheses, raw_input)

How to fix it

  • Define variables before using them
  • Check for typos in names
  • Add import statements for external modules
  • Check variable scope — local vs global
  • Use globals() or locals() to inspect available names

Example

NameError example
# Error example
print(user_name)
# NameError: name 'user_name' is not defined

pritn("hello")
# NameError: name 'pritn' is not defined

# Fix: define before use
user_name = "Alice"
print(user_name)  # "Alice"

# Fix: correct the typo
print("hello")

# Fix: add missing import
from datetime import datetime
now = datetime.now()

Track NameError with Checkend

Checkend automatically captures NameError errors in your Python application with full context:

  • Complete traceback with syntax highlighting
  • Request context (URL, params, headers)
  • Automatic grouping of similar errors
  • Instant notifications when errors occur

Stop debugging in production

Get full error context and fix issues faster with self-hosted error tracking.