Handling NameErrors in Python like a Pro

Filed Under: Python
NameError Python

As a developer, I am sure you have come across the ‘NameErrror: name is not defined‘ error. At times it might even confuse you about what to do next? No need to worry! This tutorial will cover why the NameErrors are raised and how one can solve the error along with a few examples.

Also Read: Python ValueError Exception Handling Examples


What are NameErrors?

NameErrors are one of the most common types of Python errors and they come into the picture when you try to use a variable or a function that is not valid.

Python has a habit of running the code using the top-bottom approach. In order to access a variable, you need to make sure that you declare the variable before its usage.


Possible Reasons and Solutions Behind NameErrors

Let’s start off by looking at a few possible reasons for this error in the next section.

Reason 1 – Misspelled Variable/Function Name

As humans it is very easy for us to make spelling mistakes and even after the wrong spelling, we are able to understand the word. But Python does not have this capability. It will only understand names that you have spelled correctly.

Consider the code below. When we execute the code, a nameerror we will get nameerror on the screen.

JournalDev_Topics = ["Python", "Java", "Linux","QuickExcel"]
print(JournelDev_Topics)
NameError Cause1 Output
NameError Cause1 Output

Solution: You must first check to make sure that the spelling of the variable or function is accurate and correct.

JournalDev_Topics = ["Python", "Java", "Linux","QuickExcel"]
print(JournalDev_Topics)

Reason 2 – Using a function/variable before it’s declared

You must declare a variable/function before you want to use them. Again, this is because Python reads code from top-to-bottom. 

print(JournalDev_Topics_list)
JournalDev_Topics_list = ["Python", "Java", "Linux","QuickExcel"]
NameError Cause2 Output
NameError Cause2 Output

Solution: If the error occurs due to this reason, we fix it by moving the declaration above the print statement.

JournalDev_Topics_list = ["Python", "Java", "Linux","QuickExcel"]
print(JournalDev_Topics_list)

Also Read: Python KeyError Exception Handling Examples

Reason 3 – Variable/function not defined

As your code gets larger, it is easy to forget to define a variable completely and that results in name error. Have a look at the code below. In the code, the c variable was never declared and is missed out.

a = 3
b = 6
a += b
b += c

Solution: To solve this problem, we need to declare the “c” variable before we use it in our code like below.

a = 3
b = 6
c = 1
a += b
b += c

Reason 4 – Calling an Out Of Scope Variable

There are two variable scopes: local and global variables. Local variables are only accessible in the function we declare them and Global variables are accessible throughout a whole code. If you try to access a local variable outside the function, an error comes on the screen.

Also Read: Global Variables in Python

def JournalDev():
    sent = "Hi, I love reading tutorials here!"
    x = sent.split()
    print(x)

print(sent)

While we have declared the variable “sent” inside our JournalDev() function. This means the variable is not accessible to the rest of our program. And an error occurs.

NameError Cause4 Output
NameError Cause4 Output

Solution: To solve this problem, we can declare the variable in our main program as a global variable.

sent = "Hi, I love reading tutorials here!"
def JournalDev():
    x = sent.split()
    print(x)

JournalDev()
print(sent)

Conclusion

NameErrors can seem intimidating but there are not complicated at all. I hope after reading this tutorial you are now clear with how to handle them efficiently in the Python programming language.

Thank you for reading!

close
Generic selectors
Exact matches only
Search in title
Search in content