[Python Tips] How to read a traceback

in #pythontips2 years ago (edited)

This is a new post in my ongoing Python Tips Series. You can start anywhere in this series and read whatever you find that may help you.

My Python Tips Series


How to read a traceback

Do you know how to read a traceback? They can be very confusing and overwhelming, but they can provide valuable time-saving feedback in your time of need. Knowing how to read them will be a critical skill as a python developer. Spend a few minutes with me to learn how to read them properly.

What is a traceback

A traceback provides information on the stack trace when an error happens in a Python application.

Ok then, what's a stack trace?

Yeah, I kind of saw this question coming.

A stack trace creates a stack of errors in your code so the debugger can report on them. The stack trace is able to follow nested functions and properly report where errors occur.

Let's do an example:

def start():
    run_function()

def run_function():
    do_work()

def do_work():
    1/0

start()

This code is really simple to read and the problem with it should be fairly obvious.

We start with a function called start() that then calls run_function(). run_function() does one thing, call do_work(). In do_work() we have a classic programmer problem, a divide by zero.

If I run this code, you should know we will have an error. Let's give it a try.

Traceback (most recent call last):
  File "temp.py", line 10, in <module>
    start()
  File "temp.py", line 2, in start
    run_function()
  File "temp.py", line 5, in run_function
    do_work()
  File "temp.py", line 8, in do_work
    1/0
ZeroDivisionError: division by zero

As expected we run into the ZeroDivisionError error which clearly says we tried to divide by zero.

Let's break down this traceback to get to the root of the problem.

Starting at the top we see File "temp.py", line 10, in <module> this tells us we have an error in our temp.py file in . As we are not in a function, this is a generic location. The line number is very helpful though, and the second line prints out the code that ran into a problem.

start()

You may be thinking, well there is no error in start(), it simply calls a function and there is nothing syntactically wrong with that. This is why we refer to it as a stack trace. The entire path of execution is placed on a stack so it can be reported.

So why is start() being reported as the source of the error?

Because we are dealing with an exception, the exception happens when we divide by 0 on line 8 but we do not have any code to catch that exception, so the exception is pushed up the stack until something catches it or the application crashes.

Let's check out the next two lines.

  File "temp.py", line 2, in start
    run_function()

Here we can see we had another error reported, this time in the run_function() function inside start() on line 2. As run_function() calls do_work() and do_work() raises an exception, this just reports the exception when it repeatedly got fired but nothing catches it.

The next two lines will get us closer to the real error.

  File "temp.py", line 5, in run_function
    do_work()

Now we are looking at run_function() part of the code but still really no closer to know what's going on with our code as there is obviously no problems here.

Let's keep digging.

  File "temp.py", line 8, in do_work
    1/0
ZeroDivisionError: division by zero

Well looks like we finally figured out what is wrong with our code. On line 8 in the do_work() function, we tried to execute 1/0 the infamous divide by zero every programmer knows about but ultimately tries to do attempt.

The conclusion of the stack trace gives us a critical piece of information to solve the mystery of our bad code.

ZeroDivisionError: division by zero

Seeing as we wrote this code purposely to throw this exception, you are probably not very surprised.

This walkthrough should help you read tracebacks easier, and understand where in your code the problem is happening.

Extra points if you started wondering why did we start at the top when the error is at the bottom.

You can find the exact error that was thrown at the bottom of the traceback but it is not always so simple to identify what is the problem is. These additional nested function calls being reported can help you find where your problem comes from.

Most of the time you can just read a traceback from the bottom and go up as you need additional information.

This tutorial should also give you some insight into why it is important to have proper exception handling code. If you do not catch exceptions they will just go right up your code until they interrupt the execution prematurely.


Securely chat with me on Keybase

Why you should vote me as witness

Sort:  

That’s the part of programming I really do not like. Why can’t codes just work? :)

I was wondering why you started at the top, but then you answered the question.

 2 years ago 

I was thinking it would have more impact that way.

Half of the time when I see errors like this I give up. Because usually I get errors due to using libraries incorrectly or without proper understanding.

This right here.
9/10 when libraries are not maintained, little to no documentation, or updates are done and not communicated you will always get errors in the code.
I think that is why most developers prefer doing everything from scratch.

Pro-tip: when writing a markdown code block on GitHub, you can use specify the language as python-traceback to apply syntax highlighting to python stack traces.

Helpful when opening GitHub Issues about errors!

Very instructive your publication. Thank you for the simple and clear explanation. I already have a better idea of: How to read a traceback. You motivate me to keep learning python and to grow my interest for such an important programming language.

 2 years ago 

I must say it is a great gift for python learner. Tips links at one place will make the searching easier. Your efforts are laudable. You are doing great for this community.
Your post has been curated with @gitplait community account because this is the kind of publications we like to see in our community.

Join our Community on Hive and Chat with us on Discord.

[Gitplait-Team]

Python for beginners. All Hivers need to learn how this works. Good post

I have picked your post for my daily hive voting initiative, Keep it up and Hive On!!

Python seems to be is one of the most versatile languages to work with. Thanks for the guide. I'll dig into it.