How can you get the call tree with Python profilers?

I just stumbled on this as well, and spent some time learning how to generate a call graph (the normal results of cProfile is not terribly informative). Future reference, here’s another way to generate a beautiful call-tree graphic with cProfile + gprof2dot + graphViz.

———————

  1. Install GraphViz: http://www.graphviz.org/Download_macos.php
  2. easy_install gprof2dot
  3. Run profile on the code.

    python -m cProfile -o myLog.profile <myScript.py> arg1 arg2 ...
    
  4. Run gprof2dot to convert the call profile into a dot file

    gprof2dot -f pstats myLog.profile -o callingGraph.dot
    
  5. Open with graphViz to visualize the graph

Here’s what the end result would look like!
Graph is color-coded- red means higher concentration of time.

Graph is color-coded- red means higher concentration of time

Leave a Comment