Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6880677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:00:18+00:00 2026-05-27T05:00:18+00:00

In debugging a Python script, I’d really like to know the entire call stack

  • 0

In debugging a Python script, I’d really like to know the entire call stack for my entire program. An ideal situation would be if there were a command-line flag for python that would cause Python to print all function names as they are called (I checked man Python2.7, but didn’t find anything of this sort).

Because of the number of functions in this script, I’d prefer not to add a print statement to the beginning of each function and/or class, if possible.

An intermediate solution would be to use PyDev’s debugger, place a couple breakpoints and check the call stack for given points in my program, so I’ll use this approach for the time being.

I’d still prefer to see a complete list of all functions called throughout the life of the program, if such a method exists.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:00:18+00:00Added an answer on May 27, 2026 at 5:00 am

    You can do this with a trace function (props to Spacedman for improving the original version of this to trace returns and use some nice indenting):

    def tracefunc(frame, event, arg, indent=[0]):
          if event == "call":
              indent[0] += 2
              print("-" * indent[0] + "> call function", frame.f_code.co_name)
          elif event == "return":
              print("<" + "-" * indent[0], "exit function", frame.f_code.co_name)
              indent[0] -= 2
          return tracefunc
    
    import sys
    sys.setprofile(tracefunc)
    
    main()   # or whatever kicks off your script
    

    Note that a function’s code object usually has the same name as the associated function, but not always, since functions can be created dynamically. Unfortunately, Python doesn’t track the function objects on the stack (I’ve sometimes fantasized about submitting a patch for this). Still, this is certainly “good enough” in most cases.

    If this becomes an issue, you could extract the “real” function name from the source code—Python does track the filename and line number—or ask the garbage collector find out which function object refers to the code object. There could be more than one function sharing the code object, but any of their names might be good enough.

    Coming back to revisit this four years later, it behooves me to mention that in Python 2.6 and later, you can get better performance by using sys.setprofile() rather than sys.settrace(). The same trace function can be used; it’s just that the profile function is called only when a function is entered or exited, so what’s inside the function executes at full speed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to see the call stack while debugging python in Pydev?
I'm debugging from the python console and would like to reload a module every
Debugging a PHP program, is there any add-on/plug-in for browser which I can view
I am debugging a Python program in Eclipse. I have a breakpoint in a
Is there a way for a python script to automatically detect whether it is
I need help debugging some odd file behavior in Python. Take the following script
[Edit 2: More information and debugging in answer below...] I'm writing a python script
Is there a simple way to run a Python script on Windows/Linux/OS X? On
I have been debugging a Python program which segfaults after receiving a KeyboardInterrupt exception.
I use TextMate to debug python script, as I like the feature of using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.