When programming in Python, it is common to encounter errors, and one such error is the “NameError: name ‘word’ is not defined” error.
This error occurs when you try to use a variable or name in your code that has not been defined or is not within the current scope.
It typically presents something like this:

This guide aims to provide a detailed explanation of the NameError and suggest possible solutions with code examples to help you resolve the issue.
Understanding the NameError
The NameError occurs when Python encounters a variable or name that it doesn’t recognize or cannot find within the current scope.
It can happen due to several reasons, including:
- Misspelling the variable name.
- Forgetting to define the variable before using it.
- Attempting to access a variable outside its scope.
Code Examples:
Misspelled Variable Name
# Incorrect code
message = "Hello, world!"
print(msg)
# Error: NameError: name 'msg' is not defined
Solution: In this case, the variable msg
is misspelled as message
.
To fix the error, change print(msg)
to print(message)
.
Undeclared Variable
# Incorrect code
print(word)
# Error: NameError: name 'word' is not defined
Solution: The variable word
has not been defined in the code.
To resolve the error, declare and initialize the variable before using it, like this:
word = "Python"
print(word) # Output: Python
Variable Scope Issue
# Incorrect code
def print_word():
print(word)
print_word()
# Error: NameError: name 'word' is not defined
Solution: The variable word
is not within the scope of the print_word()
function.
To access the variable, you can pass it as an argument or declare it as a global variable within the function, like this:
def print_word():
global word
print(word)
word = "Python"
print_word() # Output: Python
Preventing NameErrors
To prevent NameErrors in your Python code, consider the following practices:
- Check variable names: Double-check variable names for spelling errors and ensure they are consistent throughout your code.
- Define variables before use: Always declare and initialize variables before using them in your code.
- Understand variable scope: Be aware of the scope of variables and ensure they are accessible where they are needed. Consider function parameters or global variables when necessary.
How do I fix the NameError in Python?
To resolve the “NameError: name ‘x’ is not defined” error in Python, you must ensure that the variable ‘x’ is correctly defined and assigned a value before it is accessed or used. Additionally, it is important to reference the variable correctly, maintaining the proper case and spelling. Here are code examples that illustrate these points:
Defining and Assigning a Variable
# Incorrect code
print(x)
# Error: NameError: name 'x' is not defined
Solution: Before using the variable ‘x’, it must be defined and assigned a value:
x = 10
print(x) # Output: 10
Correct Case and Spelling
# Incorrect code
y = 5
print(Y)
# Error: NameError: name 'Y' is not defined
Solution: Ensure that the variable is referenced with the correct case and spelling:
y = 5
print(y) # Output: 5
By following these practices and ensuring proper variable definition, assignment, and referencing, you can successfully overcome the NameError and ensure your code runs without any issues.
What is NameError not defined in Python?
A NameError in Python refers to an error that occurs when you attempt to use a variable, function, or module that either does not exist or has not been used correctly.
It is a type of exception that is raised when the Python interpreter encounters an undefined name or identifier.
This error typically indicates that the name being referenced has not been declared or defined within the current scope.
Common Mistakes Leading to NameError
- Using Undefined Variables or Functions:
When you try to use a variable or function that has not been defined or declared, a NameError is raised. It is crucial to ensure that all variables and functions are properly defined before using them in your code. Example:
# Incorrect code
print(x) # Error: NameError: name 'x' is not defined
# Corrected code
x = 10
print(x) # Output: 10
- Misspelled or Incorrect Names:
Misspelling a variable, function, or module name can also result in a NameError. Python is case-sensitive, so it is essential to ensure that the name is spelled correctly and matches the case used when defining the name. Example:
# Incorrect code
my_variable = 42
print(My_variable) # Error: NameError: name 'My_variable' is not defined
# Corrected code
print(my_variable) # Output: 42
- Scope-related Issues:
NameErrors can occur when trying to access a variable or function outside of its scope. Each variable or function has a specific scope within which it is accessible, and attempting to access it from outside that scope can lead to a NameError. Example:
# Incorrect code
def my_function():
print(my_variable)
my_function() # Error: NameError: name 'my_variable' is not defined
# Corrected code
def my_function():
my_variable = 42
print(my_variable)
my_function() # Output: 42
By avoiding these common mistakes, such as using undefined variables or functions, ensuring correct spelling and casing, and considering variable and function scopes, you can prevent NameErrors and ensure your Python code runs smoothly.
How Do You Fix a Variable Not Defined in Python?
To fix a variable not defined error in Python, you need to ensure that the variable is properly defined and assigned a value before it is used. Here are some solutions with code examples:
Define and Assign a Value to the Variable
If you encounter a variable not defined error, make sure to declare the variable and assign a value to it before using it in your code.
Example:
# Incorrect code
print(x) # Error: NameError: name 'x' is not defined
# Corrected code
x = 10
print(x) # Output: 10
Check Variable Scope
Verify that the variable is within the appropriate scope for its usage. If a variable is defined within a function or conditional block, it may not be accessible outside of that scope.
Example:
# Incorrect code
def my_function():
result = 42
print(result) # Error: NameError: name 'result' is not defined
# Corrected code
def my_function():
result = 42
return result
print(my_function()) # Output: 42
Correct Spelling and Casing
Ensure that the variable name is spelled correctly and matches the casing used when it was defined. Python is case-sensitive, so even a minor difference in spelling or casing can lead to a variable not defined error.
Example:
# Incorrect code
myVariable = 42
print(myvariable) # Error: NameError: name 'myvariable' is not defined
# Corrected code
myVariable = 42
print(myVariable) # Output: 42
By following these guidelines and addressing any variable not defined errors in your code using the appropriate solutions, you can fix the issue and ensure your Python program runs successfully.
Wrapping Up
The NameError in Python occurs when a variable or name is not defined or accessible within the current scope.
By understanding the possible causes and applying the appropriate solutions, you can resolve this error in your code.
Remember to double-check variable names, define variables before use, and consider variable scope to prevent NameErrors in the future.

Abhinav worked as a software engineer at numerous startups and large enterprises for over 12 years. He has worked on a variety of projects, from developing software to designing hardware. He is passionate about tinkering with computers and learning new things. He is always looking for new ways to use technology to solve problems and make people’s lives easier. That is the inspiration behind https://foxrunsoftware.net. Abhinav created FoxRunSoftware to address common errors and issues faced by engineers and non-engineers alike!