Handling NameError Exception in Python

Last Updated : 1 Jul, 2026

A NameError occurs when Python cannot find a variable, function, or identifier that is being referenced in the program. This usually happens when a name is misspelled, used before being defined or accessed outside its valid scope.

Python
print(user_name)

Output

ERROR!
Traceback (most recent call last):
File "<main.py>", line 1, in <module>
NameError: name 'user_name' is not defined

Explanation: user_name has not been defined anywhere in the program. Since Python cannot find the variable, it raises a NameError.

Common Causes of NameError

1. Misspelt Function or Variable Name: A small spelling mistake can cause Python to look for a name that does not exist.

Python
name = "John"
pritn(name)

Output

ERROR!
Traceback (most recent call last):
File "<main.py>", line 2, in <module>
NameError: name 'pritn' is not defined. Did you mean: 'print'?

Explanation: built-in function print() is misspelled as pritn(). Since no function named pritn exists, Python raises a NameError.

2. Using an Undefined Variable: Trying to use a variable before creating it results in a NameError.

Python
age = 25
print(user_age)

 Output

ERROR!
Traceback (most recent call last):
File "<main.py>", line 2, in <module>
NameError: name 'user_age' is not defined

Explanation: variable age exists, but user_age has never been defined. Therefore, Python raises a NameError.

3. Using a Variable Before Definition: Python executes code from top to bottom. If a variable is used before it is created, a NameError occurs.

Python
print(city)
city = "Delhi"

Output

ERROR!
Traceback (most recent call last):
File "<main.py>", line 1, in <module>
NameError: name 'city' is not defined

Explanation: city is accessed before its assignment statement is executed, so Python cannot find the variable.

4. Incorrect Variable Scope: Variables defined inside a function are local to that function and cannot be accessed outside it.

Python
def show_name():
    name = "John"

show_name()
print(name)

Output

ERROR!
Traceback (most recent call last):
File "<main.py>", line 5, in <module>
NameError: name 'name' is not defined

Explanation: variable name exists only inside the show_name() function. It cannot be accessed outside the function's scope.

Handling NameError

1. Define Variables Before Using Them: Always create variables before accessing them.

Python
name = "John"
print(name)

Output
John

Explanation: variable name is defined before it is used, so the program executes successfully.

2. Check Function and Variable Names Carefully: Ensure that names are spelled correctly.

Python
name = "John"
print(name)

Output
John

Explanation: Using the correct function name print() prevents a NameError.

3. Handle NameError Using try-except: A try-except block can be used to catch and handle NameError gracefully.

Python
try:
    print(user_name)
except NameError:
    print("Variable is not defined.")

Output
Variable is not defined.

Explanation: statement print(user_name) raises a NameError, which is caught by the except NameError block.

4. Return Values from Functions Instead of Accessing Local Variables: Instead of accessing local variables directly, return them from the function.

Python
def get_name():
    name = "John"
    return name

print(get_name())

Output
John

Explanation: get_name() returns the value stored in the local variable, making it accessible outside the function.

Comment