← All notes

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

Sources

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

Permanent ID: iil:note:pt8e0r0t

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.},
}

Markdown

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.

All artifact files (JSON index)

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.