On this page
  1. trace
  2. Import
  3. Overview
  4. Quick Start
  5. Implementation Walkthrough
  6. 1. CLI Modes (python3 -m trace)
  7. Gotchas & Boundaries
  8. Classes & Objects

trace

Prev: tomllib | Up: stdlib_module | Next: traceback
Rel: typing, annotationlib, pydoc, pydoc_data, doctest
Ref: docs.python.org — trace
Tags: #public #python3 #stdlib


Import

import trace

Overview

Line-by-line execution tracer, statement coverage counter, and call-graph recorder (Trace, or python3 -m trace --trace/--count script.py). Use trace for zero-dependency quick debugging or basic line-count checks; for real test coverage reports (branches, HTML, CI thresholds), use coverage.py.

Quick Start

import trace

def compute(n: int) -> int:
    total = 0
    for i in range(n):
        total += i
    return total

tracer = trace.Trace(count=True, trace=False)
result = tracer.runfunc(compute, 5)
counts = tracer.results().counts
assert result == 10 and len(counts) > 0

Implementation Walkthrough

1. CLI Modes (python3 -m trace)

Gotchas & Boundaries

Classes & Objects

Linked from

Related notes