Last modified: Feb 23, 2025 By Alexander Williams
Python NameError: Name is Not Defined
Python is a powerful programming language. But errors can occur. One common error is the NameError: name is not defined. This error happens when Python cannot find a name in your code.
What is a NameError?
A NameError occurs when Python tries to use a variable or function that hasn't been defined yet. This can happen for several reasons. For example, you might have misspelled a variable name or forgotten to define it.
Understanding why this error occurs is crucial. For more details, check out our article on Why Does NameError Occur in Python?.
Common Causes of NameError
There are several common causes of the NameError. Let's look at a few examples.
Misspelled Variable Name
One common cause is misspelling a variable name. Python is case-sensitive. So, myVar
and myvar
are different.
# Example of a misspelled variable
myVar = 10
print(myvar) # This will raise a NameError
NameError: name 'myvar' is not defined
Using a Variable Before Definition
Another cause is using a variable before it is defined. Python reads code from top to bottom. So, if you try to use a variable before defining it, you'll get a NameError.
# Example of using a variable before definition
print(myVar) # This will raise a NameError
myVar = 10
NameError: name 'myVar' is not defined
Scope Issues
Scope issues can also cause a NameError. Variables defined inside a function are local to that function. If you try to use them outside, you'll get a NameError.
# Example of scope issue
def myFunction():
localVar = 5
print(localVar) # This will raise a NameError
NameError: name 'localVar' is not defined
How to Fix NameError
Fixing a NameError is usually straightforward. Here are some tips to help you resolve it.
Check for Typos
First, check for typos in your variable names. Make sure you are using the correct case and spelling.
Define Variables Before Use
Ensure that all variables are defined before you use them. Python reads code from top to bottom. So, define your variables at the beginning of your script or function.
Understand Scope
Understand the scope of your variables. If you define a variable inside a function, it is local to that function. To use it outside, you need to return it or define it globally.
For more detailed solutions, visit our guide on How to Fix NameError in Python.
Example: Fixing a NameError
Let's look at an example of how to fix a NameError.
# Incorrect code
print(myVar) # This will raise a NameError
myVar = 10
# Corrected code
myVar = 10
print(myVar) # This will work
10
Conclusion
The NameError: name is not defined is a common error in Python. It usually occurs due to typos, undefined variables, or scope issues. By understanding these causes, you can easily fix the error.
For a deeper understanding, read our article on Understanding NameError in Python.
Remember, debugging is a key skill in programming. With practice, you'll get better at identifying and fixing errors like the NameError.