What is stack trace in Python?

Definition

When a code throws an exception, Python prints a stack trace. A stack trace is also known as stack traceback, traceback, or backtrace. The Python stack trace contains a lot of information that can assist in determining the cause of a problem. To become better Python programmers, we must first understand what information a Python stack trace gives.

To put it another way, a stack trace shows all the calls made before the function that threw an error. The most relevant information is always written on the final line of a stack trace because this is where the error is reported. All of the information in the thrown error is made up of function calls that can help us to locate and fix the error quickly.

Example

The function calls made in our code just before the error are recorded in a stack trace report. When our application throws an exception, the stack trace is printed.

Let’s check the example below:

def printName(name, age):
print("Hi, ",name)
print("Are you",ag, " years old ?")
printName("Marry",20)

Explanation

The above stack trace contains a lot of information about what went wrong. It tells us about the type of error that has occured: NameError. This exception tells us we’ve referenced a variable that doesn’t exist. Here, ag is not defined. This exception generally tells us that a variable, function, or class has been referenced incorrectly.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved