NameError in Python: Causes and Fixes
A NameError in Python means you used a name that Python cannot find.
That name might be:
- A variable
- A function
- A class
- An imported module
This error is common for beginners because Python only knows names that were already created, assigned, or imported. If the name does not exist in the current scope, Python raises NameError.
Quick fix
x = 10
print(x)
A NameError happens when Python sees a variable, function, or module name that does not exist in the current scope.
Check these first:
- Is the name spelled correctly?
- Was it defined before this line?
- Did you forget to import it?
- Did you mean to write a string with quotes?
What this error means
NameError means Python cannot find the name you used.
For example, Python will raise this error if you try to use:
- A variable that was never assigned
- A function name that does not exist
- A module that was not imported
- Text without quotes, which Python reads as a name
Always read the exact name shown in the error message first. That tells you what Python could not find.
A typical error looks like this:
print(total)
Output:
NameError: name 'total' is not defined
In this example, Python is telling you that total does not exist at that point in the program.
Simple example that causes NameError
Here is a very short example:
print(score)
score = 100
Output:
Traceback (most recent call last):
File "example.py", line 1, in <module>
print(score)
NameError: name 'score' is not defined
The problem is on the first line. score is used before it is assigned a value.
Python runs code from top to bottom, so it must see the assignment first.
Common causes
These are the most common reasons for NameError:
- Misspelled variable or function name
- Using a variable before it is assigned
- Forgetting quotes around a string value
- Forgetting to import a module or function
- Using the wrong variable name inside a loop or function
- Trying to use a name that only exists in another scope
- Running only part of a script in a notebook or interactive session
Example: misspelled name
username = "Maya"
print(usernme)
Output:
NameError: name 'usernme' is not defined
username was defined, but usernme was used.
Example: missing quotes
print(hello)
Output:
NameError: name 'hello' is not defined
If you meant text, write:
print("hello")
Example: missing import
print(math.sqrt(16))
Output:
NameError: name 'math' is not defined
This happens because math was never imported.
If you are still learning how variables work, see Python variables explained for beginners. If the problem is related to imports, see how import works in Python.
How to fix a NameError
Use this checklist:
- Check the spelling of the name carefully
- Define the variable before using it
- Add quotes if you meant a string
- Import the module or function before calling it
- Make sure the name exists in the current function or file
- Run the code from the top so earlier definitions are available
A good way to think about it is:
- Find the exact unknown name in the error message
- Search earlier in the file for where it should be created
- Fix the missing definition, typo, quotes, or import
- Run the program again
Example fixes
Fix a typo in a variable name
Wrong:
message = "Welcome"
print(mesage)
Correct:
message = "Welcome"
print(message)
Move the assignment above the print()
Wrong:
print(price)
price = 9.99
Correct:
price = 9.99
print(price)
Change hello to "hello" when text is intended
Wrong:
word = hello
print(word)
Correct:
word = "hello"
print(word)
Import math before using math.sqrt()
Wrong:
print(math.sqrt(25))
Correct:
import math
print(math.sqrt(25))
Output:
5.0
NameError vs UnboundLocalError
These two errors are related, but they are not the same.
NameError
NameError means the name does not exist at all in the current lookup path.
Example:
print(count)
If count was never defined, Python raises NameError.
UnboundLocalError
UnboundLocalError means Python thinks a variable is local to a function, but you used it before assigning a value inside that function.
Example:
count = 10
def show_count():
print(count)
count = 20
show_count()
This raises UnboundLocalError, not NameError.
If you are not sure which one you have, read UnboundLocalError: local variable referenced before assignment fix.
Beginner debugging steps
When you see NameError, follow these steps:
- Look at the exact line number in the traceback
- Find the unknown name on that line
- Search earlier in the file for where it should be defined
- Check for typos, missing imports, and missing quotes
- Test a small fix and run the file again
These commands can also help while debugging:
python your_script.py
Run the whole script from the top. This helps when the problem is caused by missing earlier definitions.
print(variable_name)
print(type(variable_name))
dir()
help(name)
Use them carefully:
print(variable_name)can help if the variable existsprint(type(variable_name))shows what kind of value it isdir()shows names available in the current scopehelp(name)gives information about a function, class, or module
Be aware that print(variable_name) and help(name) will also fail if the name truly does not exist yet.
Common mistakes
Some beginner mistakes cause NameError again and again:
- Typing a variable name incorrectly
- Using a variable before assigning it
- Forgetting to put quotes around text
- Forgetting an import statement
- Using a local name outside its scope
- Running only part of a script in an interactive environment
For example, this can happen in a notebook:
# Cell 2
print(total)
If you forgot to run the cell that creates total, Python will raise NameError.
If your error message is specifically name 'x' is not defined, see the more focused guide: NameError: name is not defined fix.
FAQ
What is the difference between NameError and SyntaxError?
SyntaxError means the code is written in an invalid way. NameError means the code syntax is valid, but Python cannot find a name at runtime.
Why do I get NameError for text like hello?
Python treats hello as a variable name unless you write it as a string with quotes, like "hello".
Can a missing import cause NameError?
Yes. If you use a module, function, or class without importing it first, Python may raise NameError.
If the problem is about importing a package that does not exist, you may also need ModuleNotFoundError: No module named X fix.
Why does code work in one cell but fail in another?
In notebooks or interactive sessions, a name may only exist if the cell that defines it was run first.