How Do You Read a Python Traceback?
00:00 The Python traceback contains a lot of helpful information when you’re trying to determine the reason for an exception being raised in your code. In this lesson, you’ll walk through different tracebacks in order to understand the different bits of information contained in a traceback.
00:24 It contains the exception name that was raised. After the exception name is the error message. This message usually contains helpful information for understanding the reason for the exception being raised. Further up the traceback are the various function calls moving from bottom to top, most recent to least recent.
00:41 These calls are represented by two-line entries for each call. The first line of each call contains information like the filename, line number, and module name, all specifying where the code can be found.
00:53 The second line for these calls contains the actual code that was executed. It’s worth noting here that if you’re used to seeing stack traces in other programming languages, then you’ll notice a major difference in the way that a Python traceback looks in comparison.
01:17 This is very helpful since the traceback is printed out and your terminal—or wherever you’re reading the traceback—usually ends up at the bottom of the output, giving you the perfect place to start reading the traceback.
There are a few differences between traceback output when you’re executing your code in the command line and running the code in the REPL. Let’s execute the same code in our REPL and have a look at the traceback output. Notice that in place of filenames, you get
"<stdin>" (standard in).
01:42 This makes sense since you type the code in through the standard input. Also, the executed lines of code are not displayed in the traceback. Going through some specific traceback output will help you better understand and see what information the traceback will give you.
You’ll use this code in the next few examples to illustrate the information a Python traceback gives you. Here, the
who_to_greet() function takes a value
person and either returns it or prompts for a value to return instead.
you’ll get this traceback. Once again, with a Python traceback, it’s best to work backward, moving up the output. Starting at the final line of the traceback, you can see that the exception was a
TypeError. The messages that follow the exception type, everything after the colon, give you some great information.
It tells you that
greet() was called with a keyword argument that it didn’t expect. The unknown argument name is also given to you. Moving up, you can see the line that resulted in the exception. In this case, it’s the
greet() call that you added to the bottom of
In this case, because your code isn’t using any other Python modules, you just see
<module> here, meaning that this is the file that’s being executed. With a different file and different input, you can see the traceback really pointing you in the right direction to find the issue. If you’re following along, remove the buggy
greet() call from the bottom of
greetings.py and add this file to your directory. Here, you’ve set up another Python file that’s importing your previous module,
greetings.py, and using
greet() from it.
It tells you that somewhere in the code it was expecting to work with a string, but an integer was given. Moving up, you can see the line of code that was executed, then the file and line number of the code. This time, however, instead of
<module>, you get the name of the function that was being executed,
Moving up to the next executed line of code, you can see your problematic
greet() call passing in an integer. Sometimes after an exception is raised, another bit of code catches the exception and also results in an exception.
In these situations, Python will output all exception tracebacks in the order in which they were received, once again ending in the most recently raised exception’s traceback. Since this can be a little confusing, let’s look at an example. Add a call to
greet_many() to the bottom of
Notice the line starting with
During handling in this output. In between all tracebacks, you’ll see this line. Its message is very clear: while your code was trying to handle the previous exception, another exception was raised. Note that Python’s feature of displaying the previous exceptions’ tracebacks were added in Python 3. In Python 2, you’ll only get the last exception’s traceback. You have seen this exception before, when you called
greet() with an integer.
Since you added a
1 to the list of people to greet, you can expect the same result. However, the function
greet_many() wraps the
greet() call in a
except block. Just in case
greet() results in an exception being raised,
greet_many() wants to print a default greeting.
greet() results in the
TypeError because of the bad integer input,
greet_many() handles that exception and attempts to print a simple greeting. Here, the code ends up resulting in another, similar, exception.
06:05 It’s still attempting to add a string and an integer. Seeing all of the traceback output can help you see what might be the real cause of an exception. Sometimes when you see the final exception raised, and its resulting traceback, you still can’t see what’s wrong. In those cases, moving up to previous exceptions usually gives you a better idea of the root cause.
Become a Member to join the conversation.