Using a variable or function name that hasn't been defined.
A NameError is raised when Python encounters a name (variable, function, or class) that has not been defined in the current scope. This is one of the most common beginner errors and usually indicates a typo, incorrect scope, or forgotten import.
Typical causes: using a variable before assigning it, a typo in the variable name, calling a function before it is defined in the file, forgetting to import a module, or using a variable that was defined inside a conditional branch that didn't execute.
1def greet_user():2 print(f"Hello, {usernmae}!") # Typo: should be 'username'3 4username = "Alice"5greet_user()1def greet_user(username): # Best practice: pass as parameter2 print(f"Hello, {username}!")3 4username = "Alice"5greet_user(username)6 7# Or use the global variable correctly (with correct spelling)8def greet_user_v2():9 print(f"Hello, {username}!") # Correct spelling10 11greet_user_v2()Simulate standard system builds to trigger compiler trace records and track memory crashes locally.
Inside `greet_user()`, the code references `usernmae` — a misspelling of `username`. When Python executes the function, it looks up `usernmae` in the local scope, then the enclosing scope, then the global scope. It finds nothing, so it raises `NameError: name 'usernmae' is not defined`.