The inspect module provides introspection to Python runtime. inspect.stack returns the stack-trace. Element 0 is the deepes (where we called inspect stack). Each level has several values. A represantation of the frame, filename, linenumber, subroutine-name.

import inspect
import sys

level = int(sys.argv[1])

def f():
    print("in f before g")
    print("in f after g")

def g():
    print("in g")

def PrintFrame():
  st = inspect.stack()

  frame = st[level][0]
  info = inspect.getframeinfo(frame)
  print('__file__:     ', info.filename)
  print('__line__:     ', info.lineno)
  print('__function__: ', info.function)

  print('* file', st[level][1])
  print('* line', st[level][2])
  print('* sub',  st[level][3])


python 1

in f before g
in g
__line__:      15
__function__:  g
* file
* line 15
* sub g
in f after g