NameError: name ‘x’ is not defined — causes and fixes

Python raises NameError when code uses a name that, at that moment, has never been assigned, defined, or imported in a scope it can see. The cause list is short: a typo, a missing import, a variable used outside its scope, execution reaching the use before the definition — or a string that lost its quotes.

The fix, in order

  1. Compare spellings character by character. Python names are case-sensitive and exact: userName, username and user_name are three different names. The error message quotes the name it couldn’t find — diff it against the definition.
  2. Check for the missing import. Using a library’s name without importing it (math.pi without import math) raises NameError, not an import error.
  3. Check scope. A variable assigned inside a function doesn’t exist outside it; a variable assigned in a notebook cell that never ran doesn’t exist either. In notebooks: run the cells in order, top to bottom.
  4. Check definition order. At module level, code runs top-down: calling a function above its def line (in the execution path) fails. Move definitions above their first use, or call from a main block at the bottom.
  5. Check for a string missing its quotes. print(hello) looks for a variable named hello; print("hello") prints the word.

If it still happens

If the name is defined and the spelling matches, the definition probably isn’t running: it sits in a branch that isn’t taken, a cell that wasn’t executed, or a file that isn’t the one being imported (check for a same-named file shadowing yours).

Common questions

What’s the difference between NameError and UnboundLocalError? UnboundLocalError is the local-scope special case: the name IS assigned in the function, but later than the line using it — any assignment in a function makes the name local throughout.

Why does it work in my notebook but not as a script? Notebooks keep state from every cell you’ve ever run, including deleted ones. A fresh script runs only what’s in the file — the missing definition was living in the notebook’s memory, not the code.

Related errors

AttributeError: 'NoneType' has no attribute · ModuleNotFoundError: No module named · JavaScript: ReferenceError is not defined