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
Improve your Python skills fast
The fastest way to learn programming is with lots of practice. Learn a programming
concept, then write code to test your understanding and make it stick. Try our online
interactive Python course today—it's free!
Learn more about the course
Want to get better at Python quickly? Try our
interactive lessons today! Memberships are 100%
FREE this
week only!