Skip to content

py-spy

py-spy is a sampling profiler for Python. It can attach to a running process or launch a new one to show where your program spends CPU time without restarting the program or modifying its code.

Installation and Setup

Install from PyPI:

Language: Windows PowerShell
(venv) PS> py -m pip install py-spy
(venv) PS> py-spy --version
Language: Shell
(venv) $ python -m pip install py-spy
(venv) $ py-spy --version

Key Features

  • Attaches to running Python processes or launches new ones without modifying code or restarting.
  • Provides low-overhead sampling, runs outside the target process, and is suitable for production.
  • Offers live views and offline recordings that generate flame graphs, speedscope profiles, Chrome trace files, and raw sample data.
  • Dumps the current call stack of every thread in a running process, which helps you inspect a hung or stuck program without recording a profile.
  • Supports profiling native extension frames with --native and for following child processes with --subprocesses.
  • Provides flexible filtering and control including sampling rate, GIL holders only, idle threads, and per thread details.

Usage

Profile a running process and write a flame graph:

Language: Shell
$ py-spy record -o profile.svg -p <PID>

Launch a program under the profiler:

Language: Shell
$ py-spy record -o profile.svg -- python script.py

Watch hot functions live:

Language: Shell
$ py-spy top -p <PID>
$ # Or
$ py-spy top -- python script.py

Dump the current call stacks of a running process:

Language: Shell
$ py-spy dump --pid <PID>
A person holding a notebook plugs a cable into a bank of profiling machines showing a sampling timeline, a snapshot lever, waveform readouts, a flame graph, and a Python logo.

Tutorial

Python's Sampling Profiler: Low-Overhead Profiling With profiling.sampling

Explore Python 3.15's new sampling profiler and learn low-overhead profiling of scripts, threads, and live production processes.

advanced python

For additional information on related topics, take a look at the following resources:

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 5, 2026