NameError: name is not defined
Updated
The Python error "name … is not defined" means Python found no variable, function or import by that name. Python 3.12 and later suggest the missing import.
What you see
print(sys.version)Traceback (most recent call last):
File "/home/me/project/main.py", line 1, in <module>
print(sys.version)
^^^
NameError: name 'sys' is not defined. Did you forget to import 'sys'?What it means
Python looked for the name in the function, in any enclosing function, in the module and in the builtins, and found it nowhere. Here sys is a module that was never imported.
Python 3.10 started suggesting a similar name when you made a typo. Python 3.12 also recognises standard library modules, so this example now ends with "Did you forget to import 'sys'?".
Why it happens
- A missing import, as in the example.
- A typo or wrong case.
Print,true,falseandnullare not Python: the builtins areprint,True,FalseandNone. - A name used before the line that defines it, such as a function called above its
defat module level. - A variable defined only inside a function, or only in a branch that did not run, and used outside it.
- Python 2 names in old code:
raw_input,unicode,xrange. In Python 3 they areinput,strandrange.
How to fix it
Import the module, spell the name as it was defined, and define it before it is used:
import sys
print(sys.version)
In a notebook, run the cell that defines the name first; cells share names only once they have run.
Example: a typo
message = "hello"
print(mesage)Traceback (most recent call last):
File "/home/me/project/main.py", line 2, in <module>
print(mesage)
^^^^^^
NameError: name 'mesage' is not defined. Did you mean: 'message'?| Python | Prints |
|---|---|
| 3.9 | NameError: name 'mesage' is not defined |
| 3.10–3.15 | NameError: name 'mesage' is not defined. Did you mean: 'message'? |
Run on CPython 3.9.6, 3.10.22, 3.11.17, 3.12.15, 3.13.17, 3.14.9 and 3.15.1, on October 25, 2026.
How each Python version words it
| Python | Prints |
|---|---|
| 3.9–3.11 | NameError: name 'sys' is not defined |
| 3.12–3.15 | NameError: name 'sys' is not defined. Did you forget to import 'sys'? |
Run on CPython 3.9.6, 3.10.22, 3.11.17, 3.12.15, 3.13.17, 3.14.9 and 3.15.1, on October 25, 2026.
3.9 says only that the name is not defined. From 3.10 a typo gets "Did you mean: …?", and from 3.12 a missing standard library import gets "Did you forget to import …?".
Questions
Why does it say "Did you mean" with the wrong name?
The suggestion is the closest name that exists, by spelling. It is a guess; when it is wrong, the name you need was not defined anywhere Python looked.
What is the difference from UnboundLocalError?
NameError means the name does not exist in any scope. UnboundLocalError means the name is a local variable of the function that has not been given a value yet.
Sources
- Execution model: resolution of names, Python language reference
- What's New in Python 3.10: NameErrors, Python documentation
- What's New in Python 3.12: improved error messages, Python documentation
Read on October 25, 2026.