Loading video player…

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.

Avatar image for vss7

vss7 on July 9, 2024

Why do I not get Trace back feature? I just get . “Invalid syntax. Maybe you forgot a comma” I tried it with 3.10 and 3.12

Avatar image for Martin Breuss

Martin Breuss RP Team on July 10, 2024

@vss7 you’re getting a SyntaxError instead, which is pointing you to a potentially missing comma. Check your code, like the message is telling you, you have some invalid syntax somewhere.

Check out this tutorial for some common reasons for SyntaxError in Python.

You can also share your code in a comment here if you can’t spot it, then we can help you identify what causes the SyntaxError.

Avatar image for vss7

vss7 on July 10, 2024

Thanks for the info. I just noticed that the full Traceback info appears on errors in multi-line programs, but not in the one-line programs!!!

Avatar image for Martin Breuss

Martin Breuss RP Team on July 11, 2024

@vss7 the Traceback info also appears on single-line programs, for example try to create a file called hi.py and save the following code in it:

print("hi" + 2)

Then, run it using python hi.py. You’ll see that you get a Traceback in your console:

Traceback (most recent call last):
  File "/Users/realpython/hi.py", line 1, in <module>
    print("hi" + 2)
          ~~~~~^~~
TypeError: can only concatenate str (not "int") to str

The Traceback can be longer in longer programs, when Python traces back the error across a couple of steps, but it’s there either way.

Become a Member to join the conversation.