NameError: name is not defined (tested)
NameError: name 'x' is not defined means Python found no variable, function or import called x at the moment that line ran. We measured seven causes on Python 3.13.5, including two that look fine in the editor.
1. Typos, missing imports, used before assigned
for src in ["print(lenght)", "import math\nprint(sqrt(16))", "x = Ture", "print(nmae)\nname = 'a'"]:
try:
exec(src, {})
except NameError as e:
print(f"{src.splitlines()[-1]!r:22} -> NameError: {e}")
Output:
'print(lenght)' -> NameError: name 'lenght' is not defined
'print(sqrt(16))' -> NameError: name 'sqrt' is not defined
'x = Ture' -> NameError: name 'Ture' is not defined
"name = 'a'" -> NameError: name 'nmae' is not defined
Measured on Python 3.13.5: a misspelling, a function used without importing it (import math doesn't make sqrt available; use math.sqrt or from math import sqrt), Ture for True, and a name used on a line before the one that assigns it. In a normal traceback Python 3.10+ often adds Did you mean: ...?, but str(e) (above) doesn't include it.
2. Calling a function before its def has run
def main():
helper()
try:
main() # called before helper is defined
except NameError as e:
print("NameError:", e)
def helper():
print("helper ran")
main() # fine once the def has executed
Output:
NameError: name 'helper' is not defined
helper ran
Measured: def is a statement that runs top to bottom. A function body can mention helper before it exists, but only works if helper is defined by the time it's called. Put calls under if __name__ == "__main__": at the bottom.
3. A class referring to itself in an annotation
class Node:
def __init__(self, nxt: Node = None):
self.nxt = nxt
Output:
Traceback (most recent call last):
File "/tmp/ne/b3.py", line 1, in <module>
class Node:
def __init__(self, nxt: Node = None):
self.nxt = nxt
File "/tmp/ne/b3.py", line 2, in Node
def __init__(self, nxt: Node = None):
^^^^
NameError: name 'Node' is not defined. Did you mean: 'None'?
Measured: the class name isn't bound until the class body finishes, and the hint Did you mean: 'None'? is misleading. Quote it (nxt: "Node" = None) or add from __future__ import annotations.
4. The except-as variable is deleted after the block
def load():
try:
data = int("x")
except ValueError as err:
pass
print(err)
try:
load()
except NameError as e:
print(type(e).__name__ + ":", e)
Output:
UnboundLocalError: cannot access local variable 'err' where it is not associated with a value
Measured: Python deletes err at the end of the except block. Inside a function that surfaces as UnboundLocalError, which is a subclass of NameError. Copy it to another variable inside the block if you need it later. For np and pd specifically, see name 'np' is not defined.
Frequently asked questions
What does NameError: name is not defined mean in Python?
Python looked up the name and found nothing: a typo, a missing import, or a use before the assignment or def ran. We measured each on Python 3.13.5.
Why is a function not defined when it's in my file?
The call ran before the def statement executed. We measured main() failing when called above def helper() and working after it.
How do I reference a class inside its own definition?
Use a string annotation 'Node' or from __future__ import annotations. We measured the bare name raising NameError with a misleading Did you mean: 'None'? hint.
Why is my exception variable not defined after except?
Python deletes the as-variable at the end of the except block. We measured it raising UnboundLocalError, a NameError subclass, inside a function.
Written with AI assistance. Measured on 2026-10-09 with Python 3.13.5 on Linux: typos, a missing from-import, True misspelled, use before assignment, calling before def, a self-referencing class annotation, and reading an except-as variable after the block. Every block was run verbatim and its output pasted from that run. PlainHeaders is our extension; everything else here is built-in Python.