Python NameError
When you run Python code, you may get a NameError such as the following:
NameError: name 'x' is not defined
The x
in the error will vary depending on your program.
The error means that Python looks for something named x
but finds nothing
defined by that name.
Common causes
Common causes include:
- you misspelled a variable name
- you got the casing of a variable wrong; Python is case-sensitive
- you forgot to put quotes around a string, so Python thinks it's a variable
- you misspelled a function name, such as
pring
instead of print
- you used a variable without first having assigned a value to it
- you started using a variable before its definition
- you are trying to use a variable whose definition is only valid in a
different scope
- for example, a variable defined inside a function cannot be accessed
outside of it
- you called a function before its definition was reached