traceback.format_exc() will yield more info if that's what you want.

import traceback

def do_stuff():
    raise Exception("test exception")

try:
    do_stuff()
except Exception:
    print(traceback.format_exc())

This outputs:

Traceback (most recent call last):
  File "main.py", line 9, in <module>
    do_stuff()
  File "main.py", line 5, in do_stuff
    raise Exception("test exception")
Exception: test exception
Answer from volting on Stack Overflow
🌐
Python
docs.python.org › 3 › library › traceback.html
traceback — Print or retrieve a stack traceback
Source code: Lib/traceback.py This module provides a standard interface to extract, format and print stack traces of Python programs. It is more flexible than the interpreter’s default traceback di...
Top answer
1 of 16
1534

traceback.format_exc() will yield more info if that's what you want.

import traceback

def do_stuff():
    raise Exception("test exception")

try:
    do_stuff()
except Exception:
    print(traceback.format_exc())

This outputs:

Traceback (most recent call last):
  File "main.py", line 9, in <module>
    do_stuff()
  File "main.py", line 5, in do_stuff
    raise Exception("test exception")
Exception: test exception
2 of 16
880

Some other answer have already pointed out the traceback module.

Please notice that with print_exc, in some corner cases, you will not obtain what you would expect. In Python 2.x:

import traceback

try:
    raise TypeError("Oups!")
except Exception, err:
    try:
        raise TypeError("Again !?!")
    except:
        pass

    traceback.print_exc()

...will display the traceback of the last exception:

Traceback (most recent call last):
  File "e.py", line 7, in <module>
    raise TypeError("Again !?!")
TypeError: Again !?!

If you really need to access the original traceback one solution is to cache the exception infos as returned from exc_info in a local variable and display it using print_exception:

import traceback
import sys

try:
    raise TypeError("Oups!")
except Exception, err:
    try:
        exc_info = sys.exc_info()

        # do you usefull stuff here
        # (potentially raising an exception)
        try:
            raise TypeError("Again !?!")
        except:
            pass
        # end of useful stuff


    finally:
        # Display the *original* exception
        traceback.print_exception(*exc_info)
        del exc_info

Producing:

Traceback (most recent call last):
  File "t.py", line 6, in <module>
    raise TypeError("Oups!")
TypeError: Oups!

Few pitfalls with this though:

  • From the doc of sys_info:

    Assigning the traceback return value to a local variable in a function that is handling an exception will cause a circular reference. This will prevent anything referenced by a local variable in the same function or by the traceback from being garbage collected. [...] If you do need the traceback, make sure to delete it after use (best done with a try ... finally statement)

  • but, from the same doc:

    Beginning with Python 2.2, such cycles are automatically reclaimed when garbage collection is enabled and they become unreachable, but it remains more efficient to avoid creating cycles.


On the other hand, by allowing you to access the traceback associated with an exception, Python 3 produce a less surprising result:

import traceback

try:
    raise TypeError("Oups!")
except Exception as err:
    try:
        raise TypeError("Again !?!")
    except:
        pass

    traceback.print_tb(err.__traceback__)

... will display:

  File "e3.py", line 4, in <module>
    raise TypeError("Oups!")
🌐
Sentry
sentry.io › sentry answers › python › print stack traces in python
Print stack traces in Python | Sentry
July 3, 2026 - The function traceback.print_stack will print a stack trace at the current point in our script’s invocation. The optional limit parameter allows us to control how many entries are printed — by default, the entire stack trace will be printed.
🌐
GeeksforGeeks
geeksforgeeks.org › python › traceback-in-python
Traceback in Python - GeeksforGeeks
August 1, 2020 - traceback.print_exception(etype, value, tb, limit = None, file = None, chain = True) : Prints exception information and stack trace entries from traceback object tb to file. If tb is not None, it prints a header Traceback (most recent call last): ...
🌐
Coursera
coursera.org › tutorials › how to print, read, and format a python traceback
How to Print, Read, and Format a Python Traceback | Coursera
March 10, 2023 - Python provides a traceback when an exception is raised. You can extract, format, and print stack traces with the Python traceback module.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-print-exception-stack-trace-in-python
How to Print Exception Stack Trace in Python - GeeksforGeeks
July 15, 2025 - This helps in debugging and identifying issues quickly. ... Let's explore different methods to achieve this. traceback.print_exc() is a simple way to print the full exception stack trace directly to the console.
🌐
Jython
jython.org › jython-old-sites › docs › library › traceback.html
26.7. traceback — Print or retrieve a stack traceback — Jython v2.5.2 documentation
It exactly mimics the behavior of the Python interpreter when it prints a stack trace. This is useful when you want to print stack traces under program control, such as in a “wrapper” around the interpreter. The module uses traceback objects — this is the object type that is stored in ...
🌐
Python Module of the Week
pymotw.com › 2 › traceback
traceback – Extract, format, and print exceptions and stack traces. - Python Module of the Week
The extracted stack trace is a sequence of tuples containing the filename, line number, function name, and text of the source line. Once extracted, the stack trace can be formatted using functions like format_exception(), format_stack(), etc. The format functions return a list of strings with ...
Find elsewhere
🌐
Python
docs.python.org › 3.1 › library › traceback.html
27.8. traceback — Print or retrieve a stack traceback — Python v3.1.5 documentation
Print up to limit stack trace entries from traceback. If limit is omitted or None, all entries are printed.
🌐
Python
docs.python.org › 3.4 › library › traceback.html
29.9. traceback — Print or retrieve a stack traceback — Python 3.4.10 documentation
June 16, 2019 - Print up to limit stack trace entries from traceback. If limit is omitted or None, all entries are printed.
🌐
Scaler
scaler.com › home › topics › python program to print stack trace
Python Program to Print Stack Trace - Scaler Topics
January 6, 2023 - In the following code snippet, the value of the denominator is set to 0 to view how Python print stack trace works. The traceback.print_exc() method in the except block prints the program's location, the line where the error was encountered, and the name and relevant information about the error.
🌐
University of New Brunswick
cs.unb.ca › ~bremner › teaching › cs2613 › books › python3-doc › library › traceback.html
traceback — Print or retrieve a stack traceback — Python 3.9.2 documentation
Print up to limit stack trace entries from traceback object tb (starting from the caller’s frame) if limit is positive. Otherwise, print the last abs(limit) entries. If limit is omitted or None, all entries are printed.
🌐
Python
docs.python.org › 3.8 › library › traceback.html
traceback — Print or retrieve a stack traceback — Python 3.8.20 documentation
Print up to limit stack trace entries from traceback object tb (starting from the caller’s frame) if limit is positive. Otherwise, print the last abs(limit) entries. If limit is omitted or None, all entries are printed.
🌐
Real Python
realpython.com › python-traceback
Understanding the Python Traceback – Real Python
July 29, 2019 - Tracebacks are known by many names, ... 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....
🌐
Delft Stack
delftstack.com › home › howto › python › python print stack trace
How to Print Stack Trace in Python | Delft Stack
March 11, 2025 - This tutorial demonstrates how to print stack trace in Python. Learn various methods including using the traceback and sys modules, customizing stack trace output, and logging errors effectively. Enhance your debugging skills with practical examples and clear explanations tailored for both ...
🌐
Read the Docs
stackless.readthedocs.io › en › 2.7-slp › library › traceback.html
28.10. traceback — Print or retrieve a stack traceback — Stackless-Python 2.7.15 documentation
It exactly mimics the behavior of the Python interpreter when it prints a stack trace. This is useful when you want to print stack traces under program control, such as in a “wrapper” around the interpreter. The module uses traceback objects — this is the object type that is stored in ...
🌐
Chennai Mathematical Institute
cmi.ac.in › ~madhavan › courses › prog2-2015 › docs › python-3.4.2-docs-html › library › traceback.html
29.9. traceback — Print or retrieve a stack traceback — Python 3.4.2 documentation
It exactly mimics the behavior of the Python interpreter when it prints a stack trace. This is useful when you want to print stack traces under program control, such as in a “wrapper” around the interpreter. The module uses traceback objects — this is the object type that is stored in ...
🌐
Acid & Base
sceweb.sce.uhcl.edu › helm › WEBPAGE-Python › documentation › python_tutorial › lib › module-traceback.html
3.6 traceback -- Print or retrieve a stack traceback.
This differs from print_tb() in the following ways: (1) if traceback is not None, it prints a header "Traceback (innermost last):"; (2) it prints the exception type and value after the stack trace; (3) if type is SyntaxError and value has the appropriate format, it prints the line where the syntax error occurred with a caret indicating the approximate position of the error.
🌐
Coderz Column
coderzcolumn.com › tutorials › python › traceback-how-to-extract-format-and-print-error-stack-traces-in-python
traceback - How to Extract, Format, and Print Error Stack Traces in Python by Sunny Solanki
January 14, 2021 - Then we have caught exceptions using try-except block. We have then retrieved traceback from the error object using traceback attribute of the error object and have given it to print_tb() method which prints a stack trace.