
한국어
pyftrace
Introduction
pyftrace is a lightweight Python function tracing tool designed to monitor and report on function calls within Python scripts. It leverages sys.setprofile(Python 3.8 ~ 3.11), sys.monitoring(Python 3.12 ~) to monitor Python events and trace functions based on the results. With pyftrace, you can trace function calls across multiple modules, visualize call hierarchies, and generate execution time reports.

Key features of pyftrace include:
- Function Call/Return Tracing: Monitor calls/returns to functions in your Python script and imported modules.
- Built-in Function Tracing: Optionally trace built-in functions like
print using the --verbose flag.
- Multiple Module Support: Trace functions across multiple files within your project.
- Execution Reports: Generate reports detailing function execution times and call counts with the
--report flag.
- Path Tracing: Trace the path of traced Python file using the
--path flag.
- TUI Mode: Run pyftrace in a Text User Interface (TUI) mode using the
tui command.
- Threads and Processes: Trace the threads of a program and the processes it starts with
multiprocessing or os.fork(). Their lines are marked with [tid N] and [pid N].
- Function Filtering: Filter the function you want to track with the
--function and --exclude-function flag.
$ pyftrace --help
usage: pyftrace [options] [tui] (script | -m module) [args ...]
pyftrace: Python function tracing tool.
positional arguments:
script Path to the script to run and trace. Specify 'tui' before the script path to run in TUI mode.
options:
-h, --help show this help message and exit
-V, --version Show the version of pyftrace and exit
-v, --verbose Enable built-in and third-party function tracing
-p, --path Show file paths in tracing output
-r, --report Generate a report of function execution times
-d DEPTH, --depth DEPTH
Limit the tracing output to DEPTH
-n LINES, --max-lines LINES
Stop printing the trace after LINES lines. The program keeps running
-c, --collapse Print a call that repeats the one before it only once, followed by a repeat count
-f FUNCTION, --function FUNCTION
Trace only calls of <function> and its subcalls. Can be repeated
-e EXCLUDE_FUNCTION, --exclude-function EXCLUDE_FUNCTION
Exclude calls of <function> (and its subcalls) from tracing. Can be repeated
-F PATH, --file PATH Trace only functions defined in PATH: a file, a directory, a glob or a module name. Can be repeated
-E PATH, --exclude-file PATH
Do not trace functions defined in PATH. Can be repeated
-a, --argument Show function arguments in tracing output (Python frames only)
--format {json,text} Output format: 'text' (default) or 'json', one JSON object per line
-o OUTPUT, --output OUTPUT
Write tracing output to a file. The traced program's own output is not redirected
-m MODULE Run and trace a library module as a script, like 'python -m'.
Everything after MODULE is passed to the module.
Usage
Requirements
- Python Version: pyftrace requires Python 3.8+.
$ pyftrace [options] /path/to/python/script.py
Installation
$ git clone https://github.com/kangtegong/pyftrace.git
$ cd pyftrace
$ pip install -e .
or
$ pip install pyftrace
note: on Windows, windows-curses is required.
Use with AI Coding Agents
pyftrace ships a skill that teaches a coding agent when and how to trace
a program: how to keep the output short, how to read it, and which
options answer which question.
For Claude Code, install the skill as a plugin. At the prompt of a
session:
/plugin install pyftrace --marketplace kangtegong/pyftrace
or from a shell:
$ claude plugin install pyftrace --marketplace kangtegong/pyftrace
/plugin exists only in Claude Code in a terminal. In the VS Code
extension and other places where it answers "/plugin isn't available in
this environment", use the shell command; the skill is picked up by the
sessions started after it.
Other agents can read skills/pyftrace/SKILL.md
directly; copy it to wherever the agent looks for skills or
instructions.
The plugin installs only the skill. The agent still needs the
pyftrace command (0.4.0 or later) in the environment the project
runs in: pip install -U pyftrace.
Command-Line Options