pyprof2calltree is a Python utility that facilitates the visualization of profiling data collected with Python's standard `cProfile` module. It converts `cProfile` output into the calltree format, which can then be graphically analyzed using tools like KCachegrind or QCachegrind. The library, currently at version 1.4.5, is actively maintained with updates primarily focusing on Python version compatibility and minor improvements.
pip install pyprof2calltreeVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to profile a Python function using `cProfile`, then convert the profiling data to the KCachegrind-compatible calltree format using `pyprof2calltree.convert`. The resulting file can be opened with KCachegrind or QCachegrind for interactive visualization. An alternative `visualize()` function can attempt to launch KCachegrind directly if it's in your system's PATH.
Upgrade to a supported Python version (e.g., Python 3.7+ for latest releases) if encountering compatibility issues.
Install KCachegrind (e.g., `sudo apt install kcachegrind` on Debian/Ubuntu, `brew install qcachegrind` on macOS) or QCachegrind for your operating system.
Use `python -m pyprof2calltree` or the installed `pyprof2calltree` command-line script instead of older `eggsecutable` methods.
Always convert `cProfile` output files (e.g., `my_app.prof`) into the `callgrind` format using `pyprof2calltree` first: `pyprof2calltree -i my_app.prof -o callgrind.out`. Then open `callgrind.out` in KCachegrind.
Ensure KCachegrind or QCachegrind is installed and its executable path is included in your system's PATH. On Linux, `sudo apt install kcachegrind` or equivalent is common. On macOS, `brew install qcachegrind` is often used.
When analyzing performance, 'Self' time (or 'tottime' in `cProfile`) often indicates where the function *itself* is spending time, making it crucial for identifying bottlenecks within that specific function's code. 'Incl.' time (or 'cumtime') shows the total time spent in a function *and all its children*, which is useful for understanding the overall cost of a call path. Focus on functions with high 'Self' time for direct optimization within that function.