Skip to content

/errors/python/nameerror-name-is-not-defined

NameError: name 'foo' is not defined

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · Python 3.13.15updated
tool
Python
exit code
1
versions
Python 3.x
os
any

NameError: name 'foo' is not defined means Python looked for foo in the local, enclosing, global and built-in scopes and found nothing. The cause is almost always a typo, a missing import, or a name that is used before it is assigned or outside the function that defines it. The quoted text in the message is the exact name Python could not find.

Reproduce it

bash
cat > app.py <<'EOF'
print(foo)
EOF
python app.py

Output · exit 1

Traceback (most recent call last):
  File "/tmp/work/app.py", line 1, in <module>
    print(foo)
          ^^^
NameError: name 'foo' is not defined

Missing import

bash
cat > app.py <<'EOF'
print(os.getcwd())
EOF
python app.py

Output · exit 1

Traceback (most recent call last):
  File "/tmp/work/app.py", line 1, in <module>
    print(os.getcwd())
          ^^
NameError: name 'os' is not defined. Did you forget to import 'os'?

Fix

bash
cat > fix.py <<'EOF'
import os

def make():
    value = 42
    return value

result = make()
print(result)
print(type(os.getcwd()).__name__)
EOF
python fix.py

Output

42
str

Read the hint at the end of the message

Since Python 3.10 the interpreter compares the missing name with the names that do exist and adds a suggestion such as Did you mean: 'foo'?. Python 3.12 extended this: inside a method it suggests self.x when the instance has that attribute, and for a standard library module used without import it asks Did you forget to import 'os'?. Older versions print only the bare message.

bash
cat > typo.py <<'EOF'
foo = 1
def show():
    print(Foo)
show()
EOF
python typo.py

Output · exit 1

Traceback (most recent call last):
  File "/tmp/work/typo.py", line 4, in <module>
    show()
    ~~~~^^
  File "/tmp/work/typo.py", line 3, in show
    print(Foo)
          ^^^
NameError: name 'Foo' is not defined. Did you mean: 'foo'?

How to recognise the cause

ClueLikely cause
Did you mean: 'foo'?A typo or a capitalisation difference
Did you mean: 'self.x'?A method reads an attribute without self.
Did you forget to import 'os'?, or a module name in the messageThe import line is missing or sits below the use
A name like true, null or undefinedJSON or JavaScript spelling; use True, None
The name is assigned inside an if, try or loop that did not runThe assignment never executed on this path
The name is defined in another functionLocal variables are not visible outside their function

Prevent it

Run a linter such as pyflakes, Ruff or pylint: they report undefined names without running the code, including in branches your tests do not reach. Give every variable a value before conditional code assigns it, and import modules at the top of the file.

A variable assigned inside a function is not visible outside it: return it, as make() does above. If Python reports UnboundLocalError instead, the name is assigned somewhere in the same function; see the related page.

FAQ

What is the difference between NameError and UnboundLocalError?

NameError means the name exists in no scope at all. UnboundLocalError, a subclass of NameError, means the name is assigned somewhere in the current function, so Python treats it as local, but it is read before that assignment runs.

Why do I get NameError in Jupyter for a variable I defined?

The cell that defines it was not run in the current kernel session, for example after a kernel restart or when cells are run out of order. Run the defining cell again, or restart and run all cells from the top.

Why is name 'true' or 'null' not defined?

Python spells these True, False and None. Lowercase true, false and null come from JSON or JavaScript and are plain undefined names in Python code.

Sources