NameError: name 'x' is not defined
Python doesn’t know the name — it was never assigned/imported in this scope, or it’s misspelled.
Seen on:
Python
Meaning
Equivalent to JavaScript’s ReferenceError. Common after forgetting an import, using a variable before assignment in a branch that didn’t run, or Python 2 code (raw_input, unicode) on Python 3.
Common causes
- Missing import
- Typo or wrong case
- Variable only defined inside an if branch that didn’t run
- Python 2 names on Python 3
⚡ Quick fix
- Add the import
- Initialize the variable before branches
- Replace Python 2 built-ins (
raw_input→input)
Detailed fix by platform
Python
- Linters (ruff, pyflakes) catch undefined names before runtime.
How to diagnose
- Name — Spelled right?
- Import — Imported?
- Flow — Assigned on every path?
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 2 Oct 2026