← Back

Home › Errors › NameError

NameError: name 'true' is not defined

Exception: NameErrorCategory: runtime (name lookup)Raised at: the line that evaluates the undefined name

What this error means

true, false and null are not Python. They come from JavaScript, Java, C#, PHP and from JSON. Python spells the same three values True, False and None, and they are capitalised because in Python they are ordinary objects, not reserved keywords.

Since Python has no true, the name lookup fails and you get the same NameError you would for any other undefined name. Python 3.10+ often adds a helpful hint:

Traceback (most recent call last):
  File "demo.py", line 1, in <module>
    active = true
             ^^^^
NameError: name 'true' is not defined. Did you mean: 'True'?

The mapping

active = true          # ❌ NameError
active = True          # ✅

result = null          # ❌ NameError
result = None          # ✅

if active == true:     # ❌
if active:             # ✅ — and the Pythonic way to test a boolean

The common cause: JSON evaluated as Python

This is where most reports actually come from. A JSON response is text, and JSON's vocabulary is true/false/null. Pasting it into Python source, or feeding it to eval(), makes Python try to resolve those as names:

import json

raw = '{"active": true, "nickname": null}'

data = eval(raw)          # ❌ NameError: name 'true' is not defined
data = json.loads(raw)    # ✅ {'active': True, 'nickname': None}

print(data)               # json.loads converts true -> True, null -> None

json.loads does the vocabulary translation for you. Never use eval() on data from a file, an API or a user — besides this error, it executes arbitrary code.

Related naming traps from other languages

let x = 5            # ❌ JavaScript — Python has no declaration keyword
x = 5                # ✅

const LIMIT = 10     # ❌
LIMIT = 10           # ✅ (convention only — Python has no constants)

print len("abc")     # ❌ Python 2 — print is a function in Python 3
print(len("abc"))    # ✅

name && age          # ❌ C-style operators
name and age         # ✅

for (i = 0; i < 5; i++)   # ❌ C-style loop
for i in range(5):        # ✅

How to debug it

  1. Read the Did you mean hint — for this error it is almost always right.
  2. If the code came from a JavaScript tutorial or a Stack Overflow answer in another language, check every keyword: true, false, null, let, const, &&, ||, !.
  3. If the text is JSON, parse it with json.loads() instead of evaluating it as Python.
  4. For None comparisons use is None, not == null and not == None.
  5. Run the file through a linter (ruff or flake8) — undefined names are caught before you run the code at all.

Fix it interactively

These free problems practice booleans, None and name rules — repair them in the browser:

Practice more Python bugs →

Related messages worth knowing

🇮🇳 Hindi में समझें

NameError: name 'true' is not defined ka matlab hai aapne JavaScript ya JSON ka shabd Python me likh diya. Python me ye teen values alag likhi jaati hain: true → True, false → False, null → None — sab capital T/F/N ke saath, kyunki Python me ye keywords nahi, ordinary objects hain. Sabse common wajah: kisi API ya file ka JSON text seedha eval() kar diya ya source me paste kar diya. JSON ka vocabulary true/false/null hota hai, isliye fix hai json.loads(raw) — wo khud true ko True aur null ko None bana deta hai. eval() kabhi mat use karo, wo arbitrary code chala deta hai. Aur None check karne ke liye is None likho, == null nahi.

Python beginner गलतियाँ Hindi में पढ़ें →