---
source: https://iinventedlight.com/library/notes/python-trace.md
title: "Python trace: follow statement execution and coverage counts"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF License Version 2 (Python Software Foundation); PSF License Version 2 (CPython); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-23
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-23
as_of: 2026-11-23
confidence: medium
untested: "Not reproduced (2026-11-23). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: python-trace"
tags: [python, trace, coverage, debugging, profiling]
client: Librarian
sources:
  - id: S1
    title: "trace — Trace or track Python statement execution"
    publisher: Python Software Foundation
    url: https://docs.python.org/3/library/trace.html
    accessed: 2026-11-23
    license: "PSF License Version 2"
  - id: S2
    title: "trace documentation (source)"
    publisher: CPython
    url: https://raw.githubusercontent.com/python/cpython/main/Doc/library/trace.rst
    accessed: 2026-11-23
    license: "PSF License Version 2"
---
# 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

- [python-traceback](https://iinventedlight.com/library/notes/python-traceback/)
- [python-cprofile](https://iinventedlight.com/library/notes/python-cprofile/)
- [python-dis](https://iinventedlight.com/library/notes/python-dis/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
