Python Reference ยท Exceptions
Python NameError and UnboundLocalError
Reference for Python NameError and UnboundLocalError: undefined names, typos, scope mistakes, the global and nonlocal fixes, and del or except side effects.
NameError is raised when Python looks for a variable, function or class by name and finds it nowhere: not in the local scope, not in an enclosing function, not at module level, not in the built-ins. The message reads name 'total' is not defined.
UnboundLocalError is a subclass of NameError. It appears when a name is local to a function (because the function assigns to it somewhere) but is read before any assignment has happened.
print(totl) # NameError: name 'totl' is not defined def bump(): count += 1 # UnboundLocalError: cannot access local variable 'count' ...
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Syntax
try: use(some_name) except NameError as error: print(error.name)
| Name | Description |
|---|---|
name |
The name that could not be found (added in Python 3.10). |
Raises when: a name lookup fails in every scope (NameError), or a local name is read before it is bound (UnboundLocalError).
Usage notes
- Typos and case.
Totalandtotalare different names. This is the most common cause and the traceback often suggests the closest name. - Order matters. A function or class must be defined before the line that uses it runs. Defining a function after the call that needs it fails at that call, though a function body may refer to names defined later as long as they exist when it is called.
- Any assignment makes a name local. If a function assigns to
countanywhere (including+=,for count in ...orimport count), every use inside that function refers to the local one. Reading it first raisesUnboundLocalError. - Fixes for UnboundLocalError: pass the value in as a parameter and return the result (best), or declare
global nameornonlocal namewhen you truly need to rebind an outer variable. Reading an outer name without assigning it needs no declaration. delandexcept ... as name. Afterdel xthe name is gone. The name bound byexcept ... as caughtis deleted when theexceptblock ends, so using it afterwards raisesNameError; copy it to another name inside the block if you need it.- Missing imports. Using
math.sqrtwithoutimport mathgivesname 'math' is not defined. - Comprehension and class scopes. A class body is not an enclosing scope for the methods inside it, so a method must use
self.attrorClassName.attrrather than the bare class-level name. - Python 3.11 and later word the local case as
cannot access local variable 'x' where it is not associated with a value; older versions saidlocal variable 'x' referenced before assignment. - Don't catch it to hide bugs.
NameErroralmost always means a programming mistake, not a runtime condition.