Fix Python NameError: name 'x' is not defined: 5 Patterns and Fixes
Python NameError means a variable, function, or module doesn't exist in the current scope when Python tries to use it. Here are the 5 patterns that cause almost every NameError, with exact fixes and code examples for each.
Python throws NameError when you reference a name that does not exist in the current scope at the time of execution. The name could be a variable, function, class, or module.
Python looks up names in this order:
- Local scope (inside the current function)
- Enclosing scope (outer functions, for closures)
- Global scope (module level)
- Built-ins (len, print, range, etc.)
If the name is not found in any of these four places, you get NameError.
Pattern 1: Variable Used Before Assignment
Python does not scan ahead. The variable must be assigned before the line that uses it.
Pattern 2: Typo in Variable or Function Name
Python is case-sensitive. userName, user_name, and username are three different names. Check spelling and case. Use your IDE's autocomplete — it only suggests names that exist in scope.
Pattern 3: Variable Defined in Wrong Scope
result is local to calculate(). It does not exist in the outer scope after the function returns. Return the value instead:
Note: If you need shared state, use a class or pass values as parameters. Global variables create hidden coupling and make bugs harder to trace.
Pattern 4: Module Used Without Import
Import the module at the top of the file:
Common ones that get missed:
Pattern 5: Conditional Assignment Not Covering All Paths
Initialize with a default before the conditional so all paths are covered:
Finding the Error Fast
The stack trace always includes the file name and line number:
Go to line 23. Trace backward. Where was config supposed to be assigned? Was it inside an if block that did not run? Was it in a function that was never called? Was it in a different file that was not imported?
Tip: In VS Code, hover over any name before running the code. The editor flags unresolved names. Catches most NameError before execution.
Quick Diagnosis
| Error message | First check |
|---|---|
name 'x' is not defined | Is x assigned above this line? |
name 'pd' is not defined | Did you run import pandas as pd? |
name 'X' is not defined (capital) | Check case, Python is case-sensitive |
free variable 'x' referenced before assignment in enclosing scope | Closure scope issue, restructure without global |
FAQ
Q: Why does NameError appear only sometimes when I run the code?
A: The name is probably inside a conditional branch that does not always run. Pattern 5 above covers this exactly. Add a default assignment before any conditional that sets the variable.
Q: Can I catch NameError with a try/except?
A: You can, but you should not. NameError almost always means a bug in the code, not a runtime condition to handle. Fix the root cause instead of swallowing the error.
For NameError that appears only at runtime based on specific inputs, paste the function and the call stack into DebugAI and it will trace which conditional branch failed to assign the name.
Debug faster starting today.
Free VS Code extension. 10 sessions/day. No credit card.