NameError happens when Python reaches a name that it cannot find in the current scope, imported modules, or built-in names.
The usual causes are using a variable before assignment, misspelling a name, changing letter case, forgetting an import, or reading a local variable from the wrong scope.
To debug it, read the final traceback line, copy the missing name exactly, then search where that name should have been defined.
The NameError occurs when you try to use a variable, function, or module name that Python doesn't recognize. This means the name hasn't been defined yet, is misspelled, or is out of scope.
# Bad lookup: message is used before it exists
print(message) # NameError: name 'message' is not defined
# Fixed lookup: define message before using it
message = "Hello, World!"
print(message)
# Check for typos
username = "John"
print(username) # Not 'userName' or 'user_name'
print(total) # NameError!
total = 100
total = 100 # Define first
print(total) # Then use
user_name = "Alice"
print(username) # NameError: 'username' vs 'user_name'
firstName = "Bob"
print(firstname) # NameError: case mismatch!
user_name = "Alice"
print(user_name) # Exact match
firstName = "Bob"
print(firstName) # Exact case match
# Use consistent naming convention (snake_case recommended)
user_name = "Alice"
first_name = "Bob"
def calculate():
result = 100
print(result) # NameError: result is local to calculate()
# Return the value from the function
def calculate():
result = 100
return result
result = calculate()
print(result)
# Alternative: use global only when shared state is intentional
result = 0
def calculate():
global result
result = 100
calculate()
print(result)
# Simple case: define the value outside the function
result = 100
def calculate():
print(result) # Can read global variable
calculate()
result = math.sqrt(16) # NameError: name 'math' is not defined
data = json.loads('{"key": "value"}') # NameError!
import math
result = math.sqrt(16)
import json
data = json.loads('{"key": "value"}')
# Or import specific functions
from math import sqrt
result = sqrt(16)
from json import loads
data = loads('{"key": "value"}')
name = Alice # NameError: name 'Alice' is not defined
print(Hello) # NameError!
name = "Alice" # Add quotes for strings
print("Hello") # Strings need quotes
When NameError appears, do not guess. Read the missing name in the final traceback line, then search for that exact spelling in the file.
If the spelling is correct, check order next. Python must execute the assignment, function definition, class definition, or import before the name is used.
If the name exists inside a function, remember that local variables stay inside that function unless you return them or pass them to another function.
NameError occurs when you try to use a variable, function, or module name that hasn't been defined, is misspelled, or is out of scope.
Define the variable before using it, check for typos, ensure correct case, import required modules, or check if the variable is in the correct scope.
Check for typos, case sensitivity (Python is case-sensitive), or scope issues. The variable might be defined in a different function or block.
Explore 500+ free tutorials across 20+ languages and frameworks.