all Python 3Exceptionsexception
NameError
built-in exceptions
Raised when a variable or function name that is not defined in the current scope is used.
Syntax
raise NameError('сообщение')
Most of the time this is a typo or a read that happens before the assignment. Worth knowing its subclass UnboundLocalError: if a name is assigned anywhere inside a function, it is local throughout the whole body, so reading it earlier fails instead of falling back to the global. Since Python 3.10 the interpreter suggests close matches ("Did you mean ..."), which usually exposes a typo immediately.
Examples
print(x) # NameError: name 'x' is not defined
def f():
return undefined_var # NameError
See also
Open in the full glossarydocs.python.orgReport an inaccuracy