Borislav Hadzhiev
Wed Apr 20 2022·4 min read
Photo by Raychan
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.
Here is an example of how the error occurs.
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 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.
employee = { 'name': 'Alice', 'age': 30, } print(employee)
The Python "NameError: name is not defined" occurs for multiple reasons:
print(hello)
.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.
# ⛔️ 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.
def do_math(a, b): return a + b print(do_math(15, 15)) # 👉️ 30
Another cause of the error is forgetting to wrap a string in single or double quotes.
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.
print()
function without wrapping the string in quotes.To solve the error, wrap the string in quotes.
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.
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.
employee = { 'name': 'Alice', 'age': 30 }
The "NameError: name is not defined" is also caused if you use a built-in module without importing it.
# ⛔️ NameError: name 'math' is not defined print(math.floor(15.5))
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.
import math print(math.floor(15.5)) # 👉️ 15
The error also occurs if you try to access a scoped variable from outside.
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.
# 👇️ 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.
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
.
def get_message(): # 👇️ mark message as global global message # 👇️ change its value message = 'hello world' return message get_message() print(message) # 👉️ "hello world"
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
.
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
.
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.
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.
def outer(): # 👇️ initialize message variable message = '' def inner(): # 👇️ declares message in inner's scope message = 'hello world' print(message) inner() print(message) # 👉️ "" outer()