Python errors / NameError

NameError: name is not defined

Python reached a name it has no value for: a variable, function or module it hasn't seen created yet.

NameError: name 'total' is not defined

Check these first

  1. Spelling and capitals. total, Total and totl are three different names to Python.
  2. Order. The line that creates the name (total = 0, def, import) has to run before the line that uses it.
  3. Quotes. Text needs quotes: "Ada". Without them, Python looks for a variable called Ada.

Causes and fixes

1 A typo in the name

A name has to be written exactly as it was created, capital letters included. When Python can guess the name you meant, it says so at the end of the message.

Broken
score = 10
print(Score)
NameError: name 'Score' is not defined. Did you mean: 'score'?
Fixed
score = 10
print(score)
10

2 Used before it's created

Python runs your program from the top. A variable exists only once the line that assigns it has run, so move that line up.

Broken
print(total)
total = 10
NameError: name 'total' is not defined
Fixed
total = 10
print(total)
10

3 Text without quotes

Without quotes, Python reads a word as the name of a variable. Put text in quotes to make it a string.

Broken
name = Ada
print(name)
NameError: name 'Ada' is not defined
Fixed
name = "Ada"
print(name)
Ada

4 A module that isn't imported

Modules such as math and random aren't loaded until you import them. Put the import line at the top of the file.

Broken
print(math.sqrt(16))
NameError: name 'math' is not defined. Did you forget to import 'math'?
Fixed
import math
print(math.sqrt(16))
4.0

5 Created inside a function, used outside it

A variable created inside a function exists only while that function runs. Return the value and store it where you call the function.

Broken
def add_tax(price):
    taxed = price * 1.2

add_tax(10)
print(taxed)
NameError: name 'taxed' is not defined
Fixed
def add_tax(price):
    taxed = price * 1.2
    return taxed

taxed = add_tax(10)
print(taxed)
12.0

Other messages

More messages NameError can show. Open yours for what it means and how to fix it.

name 'np' is not defined

np, pd and plt are the short names numpy, pandas and matplotlib get when they're imported: import numpy as np, import pandas as pd, import matplotlib.pyplot as plt. That line is missing, or comes after the first use. Put it at the top of the program.

name 'sqrt' is not defined

import math gives you math.sqrt, not sqrt on its own. Write the module's name in front, or import the name itself: from math import sqrt.

Broken
import math
print(sqrt(16))
NameError: name 'sqrt' is not defined
Fixed
import math
print(math.sqrt(16))
4.0
name 'null' is not defined

Python's word for "no value" is None, with a capital N. Its true and false are True and False.

Broken
result = null
print(result)
NameError: name 'null' is not defined
Fixed
result = None
print(result)
None
name 'raw_input' is not defined

That's Python 2. In Python 3 it's input(): name = input("Your name: ").

Still stuck?