ProjectStack
python

NameError: name 'x' is not defined

Python couldn't find a variable, function, or class with the name you used. Either it was never created, it's out of scope, or there's a typo in the name.

Common causes

  • A typo in the variable name — Python names are case-sensitive (myVar ≠ myvar)
  • You're using a variable before assigning a value to it
  • The variable was defined inside a function and you're trying to use it outside
  • A required import is missing — you used a function from a module you didn't import

How to fix it

  1. Check the spelling and case of the variable name exactly
  2. Make sure the variable is defined before the line that uses it
  3. Add the missing import: import <module> or from <module> import <name>
  4. For variables defined in functions, return them or declare them global if needed

Example

Traceback (most recent call last): File "app.py", line 3, in <module> print(username) NameError: name 'username' is not defined

Printing a variable called 'username' that was never assigned a value

Have a different error?

Paste any error message into the Error Translator to get an instant explanation.