NameError: name 'X' is not defined in Python

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

NameError: name 'X' is not defined in Python #

The Python "NameError: name is not defined" occurs when we try to access a variable or function that is not defined or before it is defined. To solve the error, make sure you haven't misspelled the variable's name and access it after it has been declared.

nameerror name is not defined

Here is an example of how the error occurs.

main.py
employee = { 'name': 'Alice', 'age': 30, } # ⛔️ NameError: name 'Employee' is not defined. Did you mean: 'employee'? print(Employee) # 👈️ misspelled variable's name

The issue is that we have misspelled the variable's name. Note that the names of variables, functions and classes are case-sensitive.

To solve the error in this scenario, we have to spell the variable's name correctly.

main.py
employee = { 'name': 'Alice', 'age': 30, } print(employee)

The Python "NameError: name is not defined" occurs for multiple reasons:

  1. Accessing a variable that doesn't exist.
  2. Accessing a variable, function or class before it is declared.
  3. Misspelling the name of a variable, a function or a class (names are case-sensitive).
  4. Not wrapping a string in quotes, e.g. print(hello).
  5. Not wrapping a key of a dictionary in quotes.
  6. Using built-in modules without importing them first.
  7. Accessing a scoped variable from outside. For example, declaring a variable in a function and trying to access it from outside.

The first thing you need to make sure is that you aren't accessing a variable that doesn't exist or has not yet been defined.

main.py
# ⛔️ NameError: name 'do_math' is not defined print(do_math(15, 15)) def do_math(a, b): return a + b

The issue in the example above is that we are trying to call the do_math function before it has been declared.

To solve the error, we have to move the line that calls the function or accesses the variable after it has been declared.

main.py
def do_math(a, b): return a + b print(do_math(15, 15)) # 👉️ 30
Note that you also have to instantiate classes or call class methods after the class has been declared.

Another cause of the error is forgetting to wrap a string in single or double quotes.

main.py
def greet(name): return 'Hello ' + name # ⛔️ NameError: name 'Alice' is not defined. Did you mean: 'slice'? greet(Alice) # 👈️ forgot to wrap string in quotes

The greet function expects to get called with a string but we forgot to wrap the string in quotes, so the name 'X' is not defined error occurred.

This can also happen when passing a string to the print() function without wrapping the string in quotes.

To solve the error, wrap the string in quotes.

main.py
def greet(name): return 'Hello ' + name greet('Alice')

The error is also caused if you have a dictionary and forget to wrap its keys in quotes.

main.py
employee = { 'name': 'Alice', # ⛔️ NameError: name 'age' is not defined age: 30 # 👈️ dictionary key not wrapped in quotes }

Unless you have numeric keys in the dictionary, make sure to wrap them in single or double quotes.

main.py
employee = { 'name': 'Alice', 'age': 30 }

The "NameError: name is not defined" is also caused if you use a built-in module without importing it.

main.py
# ⛔️ NameError: name 'math' is not defined print(math.floor(15.5))
We use the math module without importing it first, so Python doesn't know what math refers to.

To solve the error, make sure to import any modules you are using.

main.py
import math print(math.floor(15.5)) # 👉️ 15

The error also occurs if you try to access a scoped variable from outside.

main.py
def get_message(): message = 'hello world' # 👈️ variable declared in function return message get_message() # ⛔️ NameError: name 'message' is not defined print(message)

The message variable is declared in the get_message function, so it isn't accessible from the outer scope.

The best way to solve the error is to declare the variable in the outer scope if you have to access it from outside.

main.py
# 👇️ declare variable in outer scope message = 'hello world' def get_message(): return message get_message() print(message) # 👉️ "hello world"

An alternative in this scenario would also be to return the value from the function and store it in a variable.

main.py
def get_message(): message = 'hello world' return message result = get_message() print(result) # 👉️ "hello world"

Another alternative would be to mark the variable as global.

main.py
def get_message(): # 👇️ mark message as global global message # 👇️ change its value message = 'hello world' return message get_message() print(message) # 👉️ "hello world"
Note that using the global keyword should generally be avoided as it makes our code harder to read and reason about.

If you are trying to access a variable that is declared in a nested function from an outer function, you can mark the variable as nonlocal.

main.py
def outer(): def inner(): message = 'hello world' print(message) inner() # ⛔️ NameError: name 'message' is not defined print(message) outer()

The inner function declares a variable named message but we try to access the variable from the outer function and get the "name message is not defined" error.

To get around this, we can mark the message variable as nonlocal.

main.py
def outer(): # 👇️ initialize message variable message = '' def inner(): # 👇️ Mark message as nonlocal nonlocal message message = 'hello world' print(message) inner() print(message) # 👉️ "hello world" outer()

The nonlocal keyword allows us to work with the local variables of enclosing functions.

Note that we had to initialize the message variable in the outer function, but we were able to change its value in the inner function.

Had we not used the nonlocal statement, the call to the print() function would have returned an empty string.

main.py
def outer(): # 👇️ initialize message variable message = '' def inner(): # 👇️ declares message in inner's scope message = 'hello world' print(message) inner() print(message) # 👉️ "" outer()

Conclusion #

The Python "NameError: name is not defined" occurs when we try to access a variable or function that is not defined or before it is defined. To solve the error, make sure you haven't misspelled the variable's name and access it after it has been declared.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.