python type_error official true

NameError

ID: python/nameerror

Also available as: JSON · Markdown
80%Fix Rate
95%Confidence
0Evidence

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

A local or global name is not defined. Usually a typo in the variable name, or referencing a variable before assignment.

generic

Official Documentation

https://docs.python.org/3/library/exceptions.html

Workarounds

  1. 90% success Check spelling of the variable/function name
    Use IDE autocomplete; ensure the variable is defined before use
  2. 90% success Check import statements and ensure the name is in scope
    Verify 'import module' or 'from module import name' is executed

Dead Ends

Common approaches that don't work:

  1. Adding a global declaration without understanding the scope 80% fail

    May create unintended side effects across modules