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).

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/Python-Traceback-Tutorial1.jpg" data-lazy- height="37" src="data:image/svg xml,” width=”612″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/Python-Traceback-Tutorial2.jpg" data-lazy- height="113" src="data:image/svg xml,” width=”610″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/Python-Traceback-Tutorial3-1.jpg" data-lazy- height="34" src="data:image/svg xml,” width=”597″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/Python-Traceback-Tutorial4.jpg" data-lazy- height="106" src="data:image/svg xml,” width=”616″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/Python-Traceback-Tutorial5.jpg" data-lazy- height="37" src="data:image/svg xml,” width=”601″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/Python-Traceback-Tutorial6.jpg" data-lazy- height="115" src="data:image/svg xml,” width=”616″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/Python-Traceback-Tutorial7-1.jpg" data-lazy- height="38" src="data:image/svg xml,” width=”605″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/Python-Traceback-Tutorial8.jpg" data-lazy- height="108" src="data:image/svg xml,” width=”615″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/Python-Traceback-Tutorial9.jpg" data-lazy- height="39" src="data:image/svg xml,” width=”610″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/Python-Traceback-Tutorial10.jpg" data-lazy- height="111" src="data:image/svg xml,” width=”610″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/Python-Traceback-Tutorial11.jpg" data-lazy- height="20" src="data:image/svg xml,” width=”607″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/Python-Traceback-Tutorial12.jpg" data-lazy- height="113" src="data:image/svg xml,” width=”612″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/Python-Traceback-Tutorial13.jpg" data-lazy- height="21" src="data:image/svg xml,” width=”608″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/Python-Traceback-Tutorial14.jpg" data-lazy- height="34" src="data:image/svg xml,” width=”618″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/Python-Traceback-Tutorial15.jpg" data-lazy- height="35" src="data:image/svg xml,” width=”599″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/Python-Traceback-Tutorial16.jpg" data-lazy- height="110" src="data:image/svg xml,” width=”616″>

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.

About the author

<img alt="Aqsa Yasin" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/Author-Image-150×150.jpg6007b6ff9bfcb.jpg" height="112" src="data:image/svg xml,” width=”112″>

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.