What does "NameError: name is not defined" mean?
A NameError means Python reached a word in your code that it reads as a name, such as a variable or a function, and nothing has been given that name yet. It's nearly always a spelling slip, some text that has lost its quotes, or a variable being used on a line above the one that creates it.
Why Python can't find the name
Any word in your code that isn't in quotes and isn't part of Python itself is a name. When a line runs, Python looks up each name in it, and if one doesn't exist yet the program stops right there. The lines above have already run, and the lines below don't run at all.
Names have to match exactly, capital letters included. Python 3.14 often adds a guess at what you meant to the end of the message.
total = 5
print(Total)
Traceback (most recent call last):
File "main.py", line 2, in <module>
print(Total)
NameError: name 'Total' is not defined. Did you mean: 'total'?
The traceback gives you the line number and shows you the line, and its last line names the word Python didn't recognise. The guess comes from comparing spellings with names that do exist, so it's worth reading carefully before you take its advice. Here's what happens when some text loses its quotes.
greeting = Morning
print(greeting)
Traceback (most recent call last):
File "main.py", line 1, in <module>
greeting = Morning
NameError: name 'Morning' is not defined. Did you mean: 'Warning'?
Warning is a name that comes built into Python, and it happens to be spelt a lot like Morning. It's no use here. The real fix is the quotes, greeting = "Morning", so Python reads the word as text.
Order matters as well. Python runs a program from the top down, so this fails on line 1 even though total gets a value on line 2. There's no suggestion this time, because nothing with a similar name exists yet.
print(total)
total = 12
Traceback (most recent call last):
File "main.py", line 1, in <module>
print(total)
NameError: name 'total' is not defined
You'll also see a NameError if you make a variable inside a function and then try to use it outside, because it only exists while the function is running. The Handbook page on scope explains that one.
Try it
This program has a NameError in it. Run it and read the last line of the traceback before you change anything.
Run it
main.pyracer = "Sam" laps = 12 print(racer, "finished", lap, "laps")
Output
Press Run and what your program prints shows up here.
Python says NameError: name 'lap' is not defined. Did you mean: 'laps'?, and nothing is printed, because the error is on the only line that prints. Change lap to laps on line 3 and run it again, and you'll get Sam finished 12 laps. Then try moving the laps = 12 line down below the print to see the same error turn up for a different reason.
A small exercise
This program should print Morning, Sam, but it has two NameErrors in it. Work out what they are before you run it, then fix them.
message = "Morning, " + name
name = "Sam"
print(mesage)
Show an answer
name = "Sam"
message = "Morning, " + name
print(message)
Line 1 uses name before it has a value, so the two lines need to swap places. The last line spells message wrong. Python only ever reports the first error it hits, so if you ran the program after fixing just the order you'd then see NameError: name 'mesage' is not defined. Did you mean: 'message'?.
In the Reading Tracebacks project you'll mend some broken scripts, a NameError among them, by reading what Python tells you. The Handbook has a page on NameError and another on reading a traceback if you want more.
The project that goes with it
Reading Tracebacks is a free project in Act 1, for Neil. It runs in your browser with nothing to install, and you don't need an account for it.