NameError
Python Programming Language
Severity: MinorWhat Does This Error Mean?
A NameError means Python found a name (a variable, function, or class) that it does not recognize. Python keeps a list of all the names you have defined. If you use a name that is not on that list — whether because you forgot to define it, misspelled it, or used it before creating it — Python raises a NameError.
Affected Models
- Python 2.x
- Python 3.x
- All Python versions
Common Causes
- Using a variable before you have assigned a value to it
- A typo in the variable or function name (Python is case-sensitive — 'Count' and 'count' are different names)
- Forgetting to import a module before using something from it
- A variable defined inside a function being used outside that function (scope problem)
- Deleting a variable with 'del' and then trying to use it afterward
How to Fix It
-
Read the error message. It says 'name X is not defined'. Find every place in your code where X is written.
Use your editor's Find feature (Ctrl+F or Cmd+F) to search for the exact name.
-
Check for typos. Python is case-sensitive. 'username', 'Username', and 'USER_NAME' are three completely different names.
Even one wrong letter causes a NameError. Compare the name in the error to what you actually typed.
-
Make sure the variable is defined BEFORE you use it. If you use it on line 10, it must be assigned a value on line 9 or earlier.
Python reads top to bottom. A variable does not exist until the line that creates it has actually run.
-
If the error involves a function from another module (like 'math.sqrt'), check that you have the import statement at the top of your file.
Example: 'import math' must come before any use of 'math.sqrt'.
-
If the variable is defined inside a function, it only exists inside that function. Move it outside the function if you need it in other places.
This is called 'variable scope'. Think of a function as a separate room — variables made inside that room cannot be seen from outside.
When to Call a Professional
NameErrors are always something you can fix yourself — no outside help needed. The error message tells you exactly which name Python cannot find. Search your code for that name to see where it is defined and where it is used.
Frequently Asked Questions
Why does Python say the name is not defined when I can clearly see it in my code?
Check for a subtle typo — even an extra capital letter counts. Also check if the variable is defined inside a function or an if block. If it is, Python can only see it in that local area. Variables defined in one function are invisible to the rest of the program.
I defined the variable at the bottom of the file. Does that matter?
Yes, it matters a lot. Python runs your code from top to bottom. If you use a variable on line 5 but define it on line 20, Python has not seen the definition yet when it reaches line 5. Move the definition above where you use it.
Can a NameError happen with function names?
Yes — the same rules apply to functions. If you call a function before defining it, or if you misspell the function name, you get a NameError. The fix is the same: make sure the function is defined before the line that calls it.