Python trace: follow statement execution and coverage counts
Answer: The trace module allows you to trace program execution and generate annotated coverage listings. It can be used in another program or from the command line. The --trace option displays lines as they are executed.
Command-line usage
The trace module can be invoked from the command line.
When you invoke trace, at least one of the main options must be specified.
--listfuncs is mutually exclusive with the --trace and --count options.
--trace displays lines as they are executed.
--listfuncs displays the functions executed by running the program.
--report produces an annotated list from an earlier program run. This does not execute any code.
--trackcalls displays the calling relationships exposed by running the program.
The Trace object
Create an object to trace execution of a single statement or expression.
All of its parameters are optional.
count enables counting of line numbers.
The trace parameter enables line execution tracing.
countfuncs enables listing of the functions called during the run.
results returns a CoverageResults object with the cumulative results of earlier calls. Does not reset the accumulated trace results.
CoverageResults should not be created directly by the user.
See also
Claims
- id: C1 claim: The module can be used from another program or from the command line. source: S1 evidence: "It can be used in another program or from the command line."
- id: C2 claim: The --trace switch displays lines as they are executed. source: S1 evidence: "Display lines as they are executed."
- id: C3 claim: Trace creates an object that can follow a single statement or expression. source: S2 evidence: "Create an object to trace execution of a single statement or expression."
- id: C4 claim: results leaves the accumulated data in place. source: S2 evidence: "Does not reset the accumulated trace results."
Sources
- [S1] trace — Trace or track Python statement execution. Python Software Foundation. https://docs.python.org/3/library/trace.html . License: PSF License Version 2. Accessed 2026-11-23.
- [S2] trace documentation (source). CPython. https://raw.githubusercontent.com/python/cpython/main/Doc/library/trace.rst . License: PSF License Version 2. Accessed 2026-11-23.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, trace — Trace or track Python statement execution; CPython, trace documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, trace — Trace or track Python statement execution; CPython, trace documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "Python trace: follow statement execution and coverage counts." iinventedlight Library, iil:note:pt8e0r0t, version of 2026-11-23. https://iinventedlight.com/library/notes/python-trace/. License: CC-BY-4.0.
@misc{iil_note_pt8e0r0t,
author = {{iinventedlight}},
title = {{Python trace: follow statement execution and coverage counts}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-23},
url = {https://iinventedlight.com/library/notes/python-trace/},
note = {Permanent ID iil:note:pt8e0r0t. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-trace.schema.json (schema; CC-BY-4.0)
- python-trace.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-trace.md.