You write a script, hit run, and Python throws NameError at you. The traceback names a symbol you thought you had defined. You scroll up, the definition is right there, and the error still fires.
You do not need to guess which case you are in. Read the traceback, look at the line number, and check each common cause in order.
Python uses the word NameError for one specific thing: you tried to use a name, and the interpreter could not find anything bound to that name in the scope it was looking in.
The fix depends on which cause actually applies.
What NameError Means
A name in Python is a label the interpreter can resolve to a value, function, class, or module. Names are created by assignment (x = 1), by def, by class, and by import.
If the interpreter hits a name and no binding exists at the time, it raises NameError: name ‘foo’ is not defined. Python is case-sensitive. print_greeting and print_greting are two different names. Spelling matters.
Python also uses lexical scoping: a name resolved inside a function looks at the function’s local scope first, then the enclosing scope, then the module scope, then the built-ins. A name only available inside one scope is invisible everywhere else.
The Four Common Causes
Almost every NameError you meet falls into one of these four buckets.
Cause 1: Calling a Function Before Defining It
print_greeting("John")
def print_greeting(name):
print("Hello " + name)
Output:
Traceback (most recent call last):
File "example.py", line 1, in
print_greeting("John")
NameError: name 'print_greeting' is not defined
Python reads a script from top to bottom. The call sits above the def, so the name does not exist yet. Move the def above the call and the error goes away.
Cause 2: Misspelling a Function Name
def print_greeting(name):
print("Hello " + name)
print_greting("John")
Output:
Traceback (most recent call last):
File "example.py", line 4, in
print_greting("John")
NameError: name 'print_greting' is not defined
The fix is the corrected spelling: print_greeting. Tab completion in your editor catches most of these before you run the script.
Cause 3: Forgetting to Import a Module
print(random.randint(1, 6))
Output:
Traceback (most recent call last):
File "example.py", line 1, in
print(random.randint(1, 6))
NameError: name 'random' is not defined
Python saw the name random but had no module bound to it. Add the import:
import random
print(random.randint(1, 6))
Output:
The exact number will vary from run to run, but the call resolves once random is imported. Imports live at the top of the file as a habit.
Cause 4: A Name That Only Exists Inside a Function
def make_greeting():
msg = "Hi"
print(msg)
Output:
Traceback (most recent call last):
File "example.py", line 4, in
print(msg)
NameError: name 'msg' is not defined
The variable msg only exists while make_greeting is running. Returning it keeps it alive outside:
def make_greeting():
msg = "Hi"
return msg
print(make_greeting())
Output:
A Reproducible Example You Can Run
Below is a script with three of the four causes in it. Run it as is. Each error you see maps directly to one of the causes above.
def fetch_user(user_id):
return {"id": user_id, "name": "Ada"}
current = fetchUser(7)
print(current["name"])
expiry = datetime.date(2026, 1, 1)
print(expiry.year)
Traceback from the second call (the typo fetchUser against the defined fetch_user):
Traceback (most recent call last):
File "example.py", line 4, in
current = fetchUser(7)
NameError: name 'fetchUser' is not defined
Fix 1: rename the call to match the definition.
Rerun. The next NameError will come from the missing import datetime. Add the import:
import datetime
expiry = datetime.date(2026, 1, 1)
Rerun. The script prints Ada followed by 2026.
When the Name Exists but Is the Wrong Kind
A subtler case looks like a NameError in the traceback but ends up being something else underneath: you shadowed a built-in name and the interpreter cannot find what you expected.
list = [1, 2, 3]
list((4, 5))
Output:
Traceback (most recent call last):
File "example.py", line 2, in
list((4, 5))
TypeError: 'list' object is not callable
Python did not raise NameError this time. It raised TypeError because the name list now points to a list object instead of the built-in type.
The same shadowing applies to id, type, input, sum, and dict. Pick a different name and the built-in comes back.
How to Debug a NameError
Read the traceback bottom-up. The last line tells you the exact name the interpreter could not find, and the line above it tells you where the lookup failed. From there, work through these checks in order.
Work through these checks in order when you hit a NameError.
- Search the file for the spelling you used at the definition site. A case mismatch is enough.
- Check whether the name sits inside a function. If it does, return it or pass it out as an argument.
- Check whether the name came from a module you forgot to import, or whether you imported the wrong symbol. The line “from module import name” brings in name, not module.
- Run the offending script under a debugger if the call site is not obvious. Set a breakpoint on the failing line and inspect the local and global scopes.
References
The official Python tutorial covers naming and binding in the execution model section, and the standard library ships with the dis module if you want to see how the compiler resolves names in compiled bytecode.
Why am I getting NameError when my function is clearly defined?
Three common reasons. The call sits above the def in the file, the call uses a different spelling or case than the def, or the name is bound inside a function and the call lives outside that scope. Read the traceback from the bottom up: the last line names the symbol, the line above it shows where Python looked.
How do I fix a NameError caused by a missing import?
Add the import at the top of the file. If you used ‘from module import name’, the symbol name is in scope but the module name is not, so calling module.name elsewhere raises NameError. Either import the module with ‘import module’ and use module.name, or import the symbol with ‘from module import name’ and call it as name.
Can a NameError come from a variable that does exist somewhere in the file?
Yes. A variable defined inside a function only lives for the duration of that function call. Reading it from outside the function raises NameError. Return the value from the function, or pass it as an argument to the code that needs it.
What is the difference between NameError and TypeError in Python?
NameError fires when a name has no binding in the current scope. TypeError fires when a name does have a binding but the operation is wrong for that object. Shadowing a built-in like list, dict, or sum with your own variable often starts as a NameError and surfaces as a TypeError when you try to call the built-in later.
How do I tell which scope a NameError came from?
Read the traceback file and line. The line shows the exact expression Python tried to evaluate. Then look up: local function scope, enclosing function scope, module global scope, built-ins. If the name lives in the module global scope, a function call that runs before the assignment has not seen the binding yet.
Also read: Python ‘Global name not defined’ Error and How to Handle It

