Pyton

Python Traceback Tutorial

Python Traceback Tutorial
Once an exception is created while executing the code, Python displays a traceback. A traceback seems to be a report in Python that includes the function calls made at a certain place in the code, i.e., once you have a mistake, it is suggested that we drop it backward (traceback). The traceback can provide details on what went down with the code if the code receives an error. Such tracebacks may look a little exhausting, but they can be really useful until you strip it down and see what it's trying to teach you. There is a lot of data in the Python traceback that will help you analyze and correct the cause for the issue being generated in your code.

Interpret the Traceback:

Looking over some tracebacks may give a better interpretation of the knowledge they provide to enable you to get something out of it. Let's take a look at the interpretation of how a basic exception can be traceback. Here is an example of a simple code generating a traceback error in Spyder's execution (Python 3).

Below is the error traceback image. The first-line shows the file location. Here are some specifications for all the lines using colors.

White: Traceback (most recent call last) is a traceback statement. On the other hand, the last line white part is showing the related error information.

Green: Telling a file name and location has an error.

Blue: Shows the line number of a file where the error has taken place.

Yellow: It displays the actual line where an exception appeared.

Red: Type of Error.

Here are a few important errors in the traceback:

NameError
IndexError
KeyError
TypeError
valueError
ImportError /ModuleNotFound

Name Error:
Whenever you want to interpret a variable that has not been specified in the code, NameError appears. Here is an easy example of NameError traceback. We have a variable 'number' defined with some value, while in the print statement, we have printed 'numb', which is not defined anywhere in the code. Let's run this code and check what happens.

You can see the NameError traceback has occurred as we haven't defined the variable 'numb', so how can it be printed out. That is why this program shows the NameError and elaborating it with extra information at the last line with white and red text. Yellow text is showing the exact code where the error occurs.

Index Error:

An IndexError is produced when a series that is out of reach is defined in the code. We have defined a list named 'new' having 5 indexes with some values in it. After that, we have to state the print command to output the value at index number 9.

When we execute this code, it will generate IndexError defining index out of range. As we have defined a list of 5 indexes, therefore the printed index number, which is 9 is unable to access because it is not in our range.

Key Error:

Python generates a key error when you try to reach the key which is not defined or mapped, especially from a dictionary. It is more like an IndexError. So, let's have a look at a simple example of a dictionary named 'random' with two keys defined in it with some values assigned to these keys. On the next line, we have printed the key named 'A' in the print statement.

Oh! We have got traceback KeyError. This is due to the wrong key provided in the print statement, which is not defined in the dictionary. One can make a mistake by providing a capital letter key while the dictionary has a small letter key defined in it.

Type Error:

TypeError is defined as an exception that occurs when some operation or method has been smeared to an unfitting type of an entity or variable. We have an example of a simple variable taking two values while this string is adding both the values. The first value is a string type, and the other is an integer type. The print statement is printing the result of the addition.

When this code is performed, it raises the exception. This exception is all about the wrong type of object being concatenated. It is elaborating that you cannot add a string type variable with an integer type variable.

Value Error:

Value Error is defined as an exception which only occurs when some in-built method takes the right type argument but the wrong value in it. Let's take a look at a little example. We are taking a built-in method of int() with some string value in a print statement.

When you execute this one-line code, it will generate a ValueError because we are using an integer type function while giving it a string value to be executed. That's why it will show that function int() has an invalid value in it.

On the other hand, if you give it some fractional value, it will convert it into an integer value.

This code outputs 11 because it takes only the integer part while the decimal part is ignored completely.

Import Error/Module Not Found:

Sometimes you have to import some packages or modules in your python code to use special functionalities through them. You will find an ImportError traceback when it is somewhat erroneous about an Import statement in the code. This traceback error occurs when you are unable to find the specific module or something from within the package. Here we have imported two modules, 'pip' and 'java', in our code.

While executing this code will give ModuleNotFoundError traceback. This is because the imported module 'java' is not supported by the python library. On the other hand, it doesn't give an exception on importing the 'pip' module because it is Python supported module.

Conclusion:

The Python traceback provides excellent knowledge that will help you figure out what's going incorrect in the code. Whether you're doing this for the first time or just don't understand what it's doing, the traceback generation can be a little daunting. To become a stronger Python programmer, learning what details a Python traceback gives is important.

Cursor jumps or moves randomly while typing in Windows 10
If you find that your mouse cursor jumps or moves on its own, automatically, randomly while typing in Windows laptop or computer, then some of these s...
How to reverse Mouse and Touchpads scrolling direction in Windows 10
Mouse and Touchpads not only make computing easy but more efficient and less time-consuming. We cannot imagine a life without these devices, but still...
How to change Mouse pointer and cursor size, color & scheme on Windows 10
The mouse pointer and cursor in Windows 10 are very important aspects of the operating system. This can be said for other operating systems as well, s...