How to backtrace a function in python 2.7?

debugging, python, python-2.7

Solution

See the traceback module.

import traceback

def foo():
    bar()

def bar():
    baz()

def baz():
    traceback.print_stack() 
    # or trace = traceback.extract_stack()

foo()

Problem

I have a big python script, with multiple files, and I need to know where a method was called. Is there a backtrace function in python like debug_backtrace in php?

Original source