PlainError

NameError: name 'x' is not defined

Last checked 2026-11-11 · 3 fixes · reviewed answer

TL;DRThis is a Python programming error that occurs when your code tries to use a variable called 'x' that has not been created or given a value yet.

Most likely cause: The variable 'x' is used before any value is assigned to it in the code

Try first: Check the code for the first use of 'x'

Likely causes

  1. The variable 'x' is used before any value is assigned to it in the codemost likely
  2. A typo where 'x' was meant to be a different variable name that does existpossible
  3. The variable 'x' exists only inside a function or loop but is referenced outside that scopeless likely

How to fix it

Safest first: SafeChanges settingsDangerous
  1. Check the code for the first use of 'x'Safe

    Open the script or file and look at the line mentioned in the error. Confirm whether a line like x = something appears before it.

  2. Add a definition for 'x' if missingChanges settings

    Insert a line that creates the variable, such as x = 0 or x = input('value'), before the line that uses it.

  3. Fix any typos in variable namesChanges settings

    Compare the spelling of 'x' on the error line with other variables in the same function or script.

If that didn't fix it

Run the code in an interactive Python shell and type x to see if it exists before the failing line

When to see a technician

If this error appears in a larger application you did not write, share the full traceback with the original developer or support team.

Quick answers

What does NameError: name 'x' is not defined mean?
This is a Python programming error that occurs when your code tries to use a variable called 'x' that has not been created or given a value yet.
What causes NameError: name 'x' is not defined?
The variable 'x' is used before any value is assigned to it in the code. (most likely) A typo where 'x' was meant to be a different variable name that does exist. (possible) The variable 'x' exists only inside a function or loop but is referenced outside that scope. (less likely)
How do I fix NameError: name 'x' is not defined?
Try these in order, safest first: 1. Check the code for the first use of 'x'. 2. Add a definition for 'x' if missing. 3. Fix any typos in variable names.
What if that didn't fix it?
Run the code in an interactive Python shell and type x to see if it exists before the failing line
When should I see a technician?
If this error appears in a larger application you did not write, share the full traceback with the original developer or support team.
Did this fix it? Report a problem

Related errors

All Coding errors →