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.
Your error
Check these first
- Spelling and capitals.
total,Totalandtotlare three different names to Python. - Order. The line that creates the name (
total = 0,def,import) has to run before the line that uses it. - Quotes. Text needs quotes:
"Ada". Without them, Python looks for a variable calledAda.
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.
score = 10
print(Score)
NameError: name 'Score' is not defined. Did you mean: 'score'?
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.
print(total)
total = 10
NameError: name 'total' is not defined
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.
name = Ada
print(name)
NameError: name 'Ada' is not defined
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.
print(math.sqrt(16))
NameError: name 'math' is not defined. Did you forget to import 'math'?
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.
def add_tax(price):
taxed = price * 1.2
add_tax(10)
print(taxed)
NameError: name 'taxed' is not defined
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.
import math
print(sqrt(16))
NameError: name 'sqrt' is not defined
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.
result = null
print(result)
NameError: name 'null' is not defined
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?
- In the traceback,
File "main.py", line 3says where the error happened. In the editor, click that line to go to it. - Go over the basics: Variables and Types
- Other errors: AttributeError, FileNotFoundError, IndentationError, IndexError, KeyError, ModuleNotFoundError, RecursionError, SyntaxError, TypeError, UnboundLocalError, ValueError, ZeroDivisionError