Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Getting the Most Out of a Python Traceback (Overview)

Python prints a traceback when an exception is raised in your code. The traceback output can be a bit overwhelming if you’re seeing it for the first time or you don’t know what it’s telling you. But the Python traceback has a wealth of information that can help you diagnose and fix the reason for the exception being raised in your code. Understanding what information a Python traceback provides is vital to becoming a better Python programmer.

By the end of this course, you’ll be able to:

  • Make sense of the next traceback you see
  • Recognize some of the more common tracebacks
  • Log a traceback successfully while still handling the exception
Download

Sample Code (.zip)

4.5 KB
Download

Course Slides (.pdf)

406.0 KB

00:00 Hi, I’m Rich Bibby with realpython.com. Python prints a traceback when an exception is raised in your code. The traceback output can be a bit overwhelming if you’re seeing it for the first time but you don’t know what it’s telling you, but the Python traceback has a wealth of information that can help you diagnose and fix the reason for the exception being raised in your code.

00:20 Understanding what information a Python traceback provides is vital to becoming a better Python programmer. By the end of this course, you’ll be able to make sense of the next traceback that you see, you’ll be able to recognize some of the more common tracebacks, and finally, you’ll be able to log a traceback successfully while still handling the exception. So, let’s get started. A traceback is a report containing the function calls made in your code at a specific point. Tracebacks are known by many names, including stack trace, stack traceback, backtrace, and maybe others. In Python, the term used is traceback. When your program results in an exception, Python will print the current traceback to help you know what went wrong.

01:03 Here is some example code to illustrate this situation. Here, the greet() function gets called with a parameter someone. However, in the greet() function, that variable name is not used. Instead, it’s been misspelled as someon in the print() call.

01:20 By the way, this tutorial assumes that you understand Python exceptions. If you’re not familiar or just want a refresher, then there are plenty of courses on Python exceptions on the Real Python site.

01:31 So, let’s see what happens when you run this program. The traceback output has all of the information that you’ll need to diagnose the issue. The final line of the traceback output tells you what type of exception was raised, along with some relevant information about that exception.

01:46 The previous lines of the traceback point out the code that resulted in the exception being raised. The exception was a NameError, which means that there was a reference to some name—variable, function, or class—that hasn’t been defined. In this case, the name referenced is someon. The final line has enough information to help you fix the problem. Searching the code for the name someon—which is a misspelling—will point you in the right direction. Often, however, your code is a lot more complicated than this example, so coming up in the next lesson, we’ll walk through some different traceback examples.

Become a Member to join the conversation.