Skip to content

Python print stack trace without exception

  • by

Use traceback.print_stack to print stack trace without exception In Python. This module provides a standard interface to extract, format, and print stack traces of Python programs.

Python print stack trace without exception

Simple example code.

import traceback


def func1():
    def func2():
        traceback.print_stack()

    func2()


func1()

Output:

Python print stack trace without exception

Here’s a way you can get some trace info:

#tracetest.py

def what():
    return 3

def hey():
    return what()

def yo():
    return hey()

import trace
tracer = trace.Trace()
tracer.run("yo()")
r = tracer.results()
r.write_results()

Output:

 --- modulename: main, funcname: <module>
<string>(1):  --- modulename: main, funcname: yo
main.py(10):     return hey()
 --- modulename: main, funcname: hey
main.py(6):     return what()
 --- modulename: main, funcname: what
main.py(2):     return 3

Do comment if you have any doubts or suggestions on this Python Traceback topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *