Friday, May 1, 2026
HomeArtificial IntelligenceUnderstanding Traceback in Python

Understanding Traceback in Python

[ad_1]

Final Up to date on December 24, 2021

When an exception happens in a Python program, usually a traceback might be printed. Realizing methods to learn the traceback will help you simply determine the error and make a repair. On this tutorial we’re going see what the traceback can let you know.

After finishing this tutorial, you’ll know:

  • Methods to learn a traceback
  • Methods to print the decision stack with out exception
  • What will not be proven within the traceback

Let’s get began.

Understanding Traceback in Python

Understanding Traceback in Python
Photograph by Marten Bjork, some rights reserved

Tutorial Overview

This tutorial is split into 4 components; they’re:

  1. The decision hierarchy of a easy program
  2. Traceback upon exception
  3. Triggering traceback manually
  4. An instance in mannequin coaching

The decision hierarchy of a easy program

Let’s think about a easy program:

This program is to print the Python dictionary information with indentations. It’s output is the next:

This can be a quick program however features are calling one another. If we add a line at the start of every perform, we are able to reveal how the output is produced with the circulate of management:

and the output might be messed with extra data:

So now we is aware of the order of how every perform is invoked. That is the concept of a name stack. At any level of time, once we run a line of code in a perform, we wish to know who invoked this perform.

Traceback upon exception

If we make one typo within the code like the next:

The typo is on the final line, which the closing bracket needs to be on the finish of line, not earlier than any +. The return worth of print() perform is a Python None object. And including one thing to None will set off an exception.

Should you run this program utilizing Python interpreter, you will notice this:

The strains beginning with “Traceback (most up-to-date name final):” is the traceback. It’s the stack of your program on the time when your program encountered the exception. Within the above instance, the traceback is in “most up-to-date name final” order. Therefore your predominant perform is at prime whereas the one triggering the exception is at backside. So we all know the problem is contained in the perform printdict().

Often you will notice the error message on the finish of the traceback. On this instance, it’s a TypeError triggered by including None and string. However the traceback’s assist stops right here. You might want to work out which one is None and which one is string. By studying the traceback, we additionally know the exception-triggering perform printdict() is invokved by indentprint(), and it’s in flip invoked by printlist(), and so forth.

Should you run this in Jupyter pocket book, the next is the output:

The knowledge is actually the identical, nevertheless it provides you the strains earlier than. and after every perform name.

Triggering traceback manually

The simplest technique to print a traceback is so as to add a increase assertion to manually create an exception. However this may even terminate your program. If we wish to print the stack at any time even with none exception, we are able to accomplish that like the next:

The road traceback.print_stack() will print the present name stack.

However certainly, we regularly wish to print the stack solely when there may be error (so we be taught extra about why it’s so). The extra widespread use case is the next:

This can be a typical sample for repeatedly calculating a perform, equivalent to Monte Carlo simulation. But when we aren’t cautious sufficient, we could run into some error, equivalent to within the above instance, we could have division by zero. The issue is, in case of extra sophisticated computation you may’t simply spot the flaw. Resembling in above, the problem buried inside the decision to compute(). Subsequently it’s useful to know how we get the error. However on the similar time we wish to deal with the case of error somewhat than let your entire program terminate. If we use the try-catch assemble, the traceback is not going to be print by default. Subsequently we’d like the use the traceback.print_exc() assertion to do it manually.

Really we are able to have the traceback extra elaborated. As a result of the traceback is the decision stack and certainly we are able to study every perform within the name stack and verify the variables in every degree. Within the sophisticated case, that is the perform I often use to do extra detailed hint:

An instance in mannequin coaching

The decision stack as reported within the traceback has a limitation: You possibly can solely see the Python features. It needs to be simply fantastic for this system you wrote however many giant libraries in Python have a part of them written in one other language and compiled into binary. An instance is Tensorflow. All of the underlying operation are in binary for the efficiency. Therefore when you run the next code, you will notice one thing totally different:

The input_shape parameter to the primary LSTM layer within the mannequin needs to be (n_in, 1) to match the enter information, somewhat than (n_in+1, 1). This code will print the next error when you invoked the final line:

Should you have a look at the traceback, you can not actually see the entire name stack. For instance, the highest body you referred to as mannequin.match() however the second body is from a perform named error_handler(). Which you can not see how the match() perform triggered that. It is because Tensorflow is very optimized. A whole lot of stuff is hidden in compiled code and never seen by the Python interpreter.

On this case, it’s important to patiently learn the traceback and discover the clue to the trigger. In fact, often the error message ought to provide you with some helpful hints as effectively.

Additional Studying

This part offers extra sources on the subject in case you are trying to go deeper.

Books

Python Official Documentation

Abstract

On this tutorial, you found methods to learn and print the traceback from a Python program.

Particularly, you discovered:

  • What data the traceback tells you
  • Methods to print a traceback at any level of your program with out elevating an exception

Within the subsequent submit, we are going to see how we are able to navigate the decision stack contained in the Python debugger.



[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments