I prefer Viztracer for Python flamegraph profiling.

See also

There are many ways to use it, and excellent documentation in the above links, but some brief notes:

$ py -m viztracer program.py

A resultant result.json will be generated.

$ vizviewer result.json

or if your Python install does not map scripts into the system PATH

$ py -m viztracer.viewer result.json

Side-by-side comparisons

You can view two reports at once to do a side-by-side analysis of two runs. Just make sure to supply --port 9002 to one of them (default port is 9001).

It also supports inline usage:

import viztracer
 
foo()
bar()
baz()
 
tracer = viztracer.VizTracer()
tracer.start()
 
whats_goin_on_in_here()
 
tracer.stop()
tracer.save()

Disable color printing

If your environment cannot handle colored printing, then add

from viztracer import util
util.color_support = False

somewhere in the pipeline. I’m not sure if there’s a CLI switch to do this or not.