In this article, we’ll show you how to fix Python nameerror name ‘x’ is not defined error message.
If you’re new to this error, it might be hard for you to resolve this error. Luckily, this article will explore the solutions to this error.
Aside from that, we’ll discuss details that will help you to understand this nameerror: name x is not defined error thoroughly.
What is “nameerror name ‘x’ is not defined”?
The nameerror name ‘x’ is not defined is an error message when you are trying to use a variable or a function named “x,” but “x” has not been defined or assigned a value.
Here are some scenario’s that you will encounter this error.
For example:
a = 5
b = 10
x = a + b + x
print(z)
Since we did not define the value of “x,” as a result, it will raise an error message.
NameError: name 'x' is not definedTo fix this error, you have to define the variable “x” before we try to use it in our expression.
a = 5
b = 10
x = 15
z = a + b + x
print(z)Output:
30Another example:
person = {
'name': 'Caren',
'age': 18,
'address': 'Korea',
'status': 'single',
}
print(Person)If you try to run this code, the the results would an error because we have misspelled the variable’s name.
NameError: name 'Person' is not defined. Did you mean: 'person'?To fix the error, we need to spell the variable’s name correctly.
person = {
'name': 'Caren',
'age': 18,
'address': 'Korea',
'status': 'single',
}
print(person)Output:
{'name': 'Caren', 'age': 18, 'address': 'Korea', 'status': 'single'}Why doe this error occur?
This error nameerror: name ‘x’ is not defined can happen because of some reasons, such as:
👎If you misspelled the variable name.
👎 If you forgot to define the variable x.
👎 If the variable is out of scope.
👎 If you are using a variable that doesn’t exist.
👎 If you are using a variable or function before it is being declared.
How to fix “nameerror name ‘x’ is not defined”?
To fix the nameerror: name ‘x’ is not defined error in Python, ensure that the variable or function named x is defined before it is used.
Solution 1: Define the variable
When the variable x is not defined, you can define it by assigning a value to it.
Variable not defined:
❌ print(x)Variable defined:
✅ x = 10
print(x)Output:
x = 10
print(x)Solution 2: Check Typos
Ensure that the variable name is spelled correctly everywhere it is used in the code.
For example:
❌ a = 20
print(a)
print(x) Solution 3: Import missing module
If x is a function or a class from a module that has not been imported, you can fix the error by importing the module.
✅ import math
x= math.sqrt(64)
print(x)2.0Solution 4: Verify the scope
Ensure that the variable x is defined in the same scope where it is being used. Variables defined inside a function or a loop are only accessible within that function or loop.
❌Variable out of scope:
def my_function():
x = 100
my_function()
print(x)✅ Variable in scope:
def my_function():
x = 100
print(x)
my_function()Conclusion
In conclusion, the nameerror name ‘x’ is not defined is an error message when you are trying to use a variable or a function named “x,” but “x” has not been defined or assigned a value.
To fix the you have to ensure that the variable or function named x is defined before it is used.
This article already provides solutions for this error to help you fix the Python error message.
You could also check out other “nameerror” articles that may help you in the future if you encounter them.
- Nameerror: name plt is not defined
- Nameerror name np is not defined
- Nameerror name ‘raw_input’ is not defined
Hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊
Function-related NameError
NameError on a function usually means the function was not defined yet, the wrong name was used, or the function is in a different scope.
Common triggers
- Function name typo.
caclulate()instead ofcalculate(). - Called before def. At module level.
- Function in wrong module. Imported from a different module than expected.
- Decorated function shadowed. Decorator returned wrong name.
Diagnostic pattern
# BAD — undefined function
def main():
result = compute_total() # NameError if compute_total not defined
main()
# GOOD — check spelling and scope
def compute_total():
return 42
def main():
result = compute_total()
print(result)
main()
# For decorated functions, make sure functools.wraps is used
from functools import wraps
def my_decorator(fn):
@wraps(fn)
def wrapper(*args, **kwargs):
return fn(*args, **kwargs)
return wrapper
Best practices
- Use editor autocomplete. Prevents most typo NameError.
- Order definitions before calls at module level.
- Use type hints. Editor flags undefined names immediately.
- Use functools.wraps in decorators to preserve the original function name.
Official documentation
Frequently Asked Questions
What is Python NameError and what causes it?
NameError is raised when Python encounters a name (variable, function, class) that hasn’t been defined in the current scope. Most common causes: typo in variable name, using a variable before assigning it, missing import, or referencing a variable that was defined inside a function but accessed outside it.
How do I fix ‘name X is not defined’?
Check three things: (1) Is the name a typo? Compare with the spelling where you defined it. (2) Did you import it? Add ‘from module import X’ or ‘import module’ at the top. (3) Is X defined in a different scope (inside a function, conditional branch, or with-block) that hasn’t executed yet at the point you’re using it? Move the definition before the use.
Why does my variable work in one cell but not another (Jupyter)?
Jupyter kernels keep state between cells. If you defined X in cell 5 and run cell 3 later, X exists. But after Kernel-Restart, only the cells you re-run define their variables. Always run cells top-to-bottom on a fresh kernel before submitting. Use ‘Restart and Run All’ to verify your notebook is reproducible.
What is the difference between NameError and AttributeError?
NameError: the name itself doesn’t exist anywhere in scope (typo, missing import, scope issue). AttributeError: the name exists and points to an object, but that object has no such attribute/method (typo on method name, wrong object type). NameError is about the variable; AttributeError is about what’s inside it.
Where can I find more NameError fixes?
Browse the NameError reference hub for 49+ specific fixes (NumPy, pandas, Jupyter, Python 2 to 3 migration). For Python scope rules see the Python Tutorial hub. For attribute-level errors see AttributeError.
