How to print the stack frames of the current execution thread of the program in Python

1 Answer

0 votes
import inspect

for frame in inspect.stack():
    print(frame)


  
'''
run:
  
FrameInfo(frame=<frame at 0x7a9216953cc0, file '<main.py>', line 4, code <module>>, filename='<main.py>', lineno=3, function='<module>', code_context=None, index=None, positions=Positions(lineno=3, end_lineno=3, col_offset=13, end_col_offset=28))
FrameInfo(frame=<frame at 0x7a9216953ed0, file '<string>', line 73, code <module>>, filename='<string>', lineno=73, function='<module>', code_context=None, index=None, positions=Positions(lineno=73, end_lineno=73, col_offset=4, end_col_offset=38))

'''

 



answered Jul 19, 2025 by avibootz

Related questions

3 answers 120 views
1 answer 225 views
1 answer 292 views
1 answer 310 views
...