parthtamu/rag-code-assistant
0
1<!DOCTYPE html>2 3<html lang="en" data-content_root="../">4 <head>5 <meta charset="utf-8" />6 <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />7<meta property="og:title" content="trace — Trace or track Python statement execution" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/trace.html" />10<meta property="og:site_name" content="Python documentation" />11<meta property="og:description" content="Source code: Lib/trace.py The trace module allows you to trace program execution, generate annotated statement coverage listings, print caller/callee relationships and list functions executed durin..." />12<meta property="og:image:width" content="1146" />13<meta property="og:image:height" content="600" />14<meta property="og:image" content="https://docs.python.org/3.15/_images/social_previews/summary_library_trace_2f9ab1fb.png" />15<meta property="og:image:alt" content="Source code: Lib/trace.py The trace module allows you to trace program execution, generate annotated statement coverage listings, print caller/callee relationships and list functions executed durin..." />16<meta name="description" content="Source code: Lib/trace.py The trace module allows you to trace program execution, generate annotated statement coverage listings, print caller/callee relationships and list functions executed durin..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20 <title>trace — Trace or track Python statement execution — Python 3.15.0a6 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">21 22 <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />23 <link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />24 <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />25 <link rel="stylesheet" type="text/css" href="../_static/profiling-sampling-visualization.css?v=0c2600ae" />26 <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />27 28 <script src="../_static/documentation_options.js?v=6b7c9ff5"></script>29 <script src="../_static/doctools.js?v=9bcbadda"></script>30 <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>31 <script src="../_static/profiling-sampling-visualization.js?v=9811ed04"></script>32 33 <script src="../_static/sidebar.js"></script>34 35 <link rel="search" type="application/opensearchdescription+xml"36 title="Search within Python 3.15.0a6 documentation"37 href="../_static/opensearch.xml"/>38 <link rel="author" title="About these documents" href="../about.html" />39 <link rel="index" title="Index" href="../genindex.html" />40 <link rel="search" title="Search" href="../search.html" />41 <link rel="copyright" title="Copyright" href="../copyright.html" />42 <link rel="next" title="tracemalloc — Trace memory allocations" href="tracemalloc.html" />43 <link rel="prev" title="timeit — Measure execution time of small code snippets" href="timeit.html" />44 45 46 <script defer file-types="bz2,epub,zip" data-domain="docs.python.org" src="https://analytics.python.org/js/script.file-downloads.outbound-links.js"></script>47 48 <link rel="canonical" href="https://docs.python.org/3/library/trace.html">49 50 51 52 53 <style>54 @media only screen {55 table.full-width-table {56 width: 100%;57 }58 }59 </style>60<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">61 <link rel="shortcut icon" type="image/png" href="../_static/py.svg">62 <script type="text/javascript" src="../_static/copybutton.js"></script>63 <script type="text/javascript" src="../_static/menu.js"></script>64 <script type="text/javascript" src="../_static/search-focus.js"></script>65 <script type="text/javascript" src="../_static/themetoggle.js"></script> 66 <script type="text/javascript" src="../_static/rtd_switcher.js"></script>67 <meta name="readthedocs-addons-api-version" content="1">68 69 </head>70<body>71<div class="mobile-nav">72 <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"73 aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">74 <nav class="nav-content" role="navigation">75 <label for="menuToggler" class="toggler__label">76 <span></span>77 </label>78 <span class="nav-items-wrapper">79 <a href="https://www.python.org/" class="nav-logo">80 <img src="../_static/py.svg" alt="Python logo">81 </a>82 <span class="version_switcher_placeholder"></span>83 <form role="search" class="search" action="../search.html" method="get">84 <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">85 <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>86 </svg>87 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q">88 <input type="submit" value="Go">89 </form>90 </span>91 </nav>92 <div class="menu-wrapper">93 <nav class="menu" role="navigation" aria-label="main navigation">94 <div class="language_switcher_placeholder"></div>95 96<label class="theme-selector-label">97 Theme98 <select class="theme-selector" oninput="activateTheme(this.value)">99 <option value="auto" selected>Auto</option>100 <option value="light">Light</option>101 <option value="dark">Dark</option>102 </select>103</label>104 <div>105 <h3><a href="../contents.html">Table of Contents</a></h3>106 <ul>107<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">trace</span></code> — Trace or track Python statement execution</a><ul>108<li><a class="reference internal" href="#command-line-usage">Command-Line Usage</a><ul>109<li><a class="reference internal" href="#main-options">Main options</a></li>110<li><a class="reference internal" href="#modifiers">Modifiers</a></li>111<li><a class="reference internal" href="#filters">Filters</a></li>112</ul>113</li>114<li><a class="reference internal" href="#programmatic-interface">Programmatic Interface</a></li>115</ul>116</li>117</ul>118 119 </div>120 <div>121 <h4>Previous topic</h4>122 <p class="topless"><a href="timeit.html"123 title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">timeit</span></code> — Measure execution time of small code snippets</a></p>124 </div>125 <div>126 <h4>Next topic</h4>127 <p class="topless"><a href="tracemalloc.html"128 title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tracemalloc</span></code> — Trace memory allocations</a></p>129 </div>130 <script>131 document.addEventListener('DOMContentLoaded', () => {132 const title = document.querySelector('meta[property="og:title"]').content;133 const elements = document.querySelectorAll('.improvepage');134 const pageurl = window.location.href.split('?')[0];135 elements.forEach(element => {136 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));137 url.searchParams.set('pagetitle', title);138 url.searchParams.set('pageurl', pageurl);139 url.searchParams.set('pagesource', "library/trace.rst");140 element.href = url.toString();141 });142 });143 </script>144 <div role="note" aria-label="source link">145 <h3>This page</h3>146 <ul class="this-page-menu">147 <li><a href="../bugs.html">Report a bug</a></li>148 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>149 <li>150 <a href="https://github.com/python/cpython/blob/main/Doc/library/trace.rst?plain=1"151 rel="nofollow">Show source152 </a>153 </li>154 155 </ul>156 </div>157 </nav>158 </div>159</div>160 161 162 <div class="related" role="navigation" aria-label="Related">163 <h3>Navigation</h3>164 <ul>165 <li class="right" style="margin-right: 10px">166 <a href="../genindex.html" title="General Index"167 accesskey="I">index</a></li>168 <li class="right" >169 <a href="../py-modindex.html" title="Python Module Index"170 >modules</a> |</li>171 <li class="right" >172 <a href="tracemalloc.html" title="tracemalloc — Trace memory allocations"173 accesskey="N">next</a> |</li>174 <li class="right" >175 <a href="timeit.html" title="timeit — Measure execution time of small code snippets"176 accesskey="P">previous</a> |</li>177 178 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>179 <li><a href="https://www.python.org/">Python</a> »</li>180 <li class="switchers">181 <div class="language_switcher_placeholder"></div>182 <div class="version_switcher_placeholder"></div>183 </li>184 <li>185 186 </li>187 <li id="cpython-language-and-version">188 <a href="../index.html">3.15.0a6 Documentation</a> »189 </li>190 191 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>192 <li class="nav-item nav-item-2"><a href="debug.html" accesskey="U">Debugging and profiling</a> »</li>193 <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">trace</span></code> — Trace or track Python statement execution</a></li>194 <li class="right">195 196 197 <div class="inline-search" role="search">198 <form class="inline-search" action="../search.html" method="get">199 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">200 <input type="submit" value="Go">201 </form>202 </div>203 |204 </li>205 <li class="right">206<label class="theme-selector-label">207 Theme208 <select class="theme-selector" oninput="activateTheme(this.value)">209 <option value="auto" selected>Auto</option>210 <option value="light">Light</option>211 <option value="dark">Dark</option>212 </select>213</label> |</li>214 215 </ul>216 </div> 217 218 <div class="document">219 <div class="documentwrapper">220 <div class="bodywrapper">221 <div class="body" role="main">222 223 <section id="module-trace">224<span id="trace-trace-or-track-python-statement-execution"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">trace</span></code> — Trace or track Python statement execution<a class="headerlink" href="#module-trace" title="Link to this heading">¶</a></h1>225<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/trace.py">Lib/trace.py</a></p>226<hr class="docutils" />227<p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">trace</span></code> module allows you to trace program execution, generate228annotated statement coverage listings, print caller/callee relationships and229list functions executed during a program run. It can be used in another program230or from the command line.</p>231<div class="admonition seealso">232<p class="admonition-title">See also</p>233<dl class="simple">234<dt><a class="reference external" href="https://coverage.readthedocs.io/">Coverage.py</a></dt><dd><p>A popular third-party coverage tool that provides HTML235output along with advanced features such as branch coverage.</p>236</dd>237</dl>238</div>239<section id="command-line-usage">240<span id="trace-cli"></span><h2>Command-Line Usage<a class="headerlink" href="#command-line-usage" title="Link to this heading">¶</a></h2>241<p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">trace</span></code> module can be invoked from the command line. It can be as242simple as</p>243<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="o">-</span><span class="n">m</span> <span class="n">trace</span> <span class="o">--</span><span class="n">count</span> <span class="o">-</span><span class="n">C</span> <span class="o">.</span> <span class="n">somefile</span><span class="o">.</span><span class="n">py</span> <span class="o">...</span>244</pre></div>245</div>246<p>The above will execute <code class="file docutils literal notranslate"><span class="pre">somefile.py</span></code> and generate annotated listings of247all Python modules imported during the execution into the current directory.</p>248<dl class="std option">249<dt class="sig sig-object std" id="cmdoption-trace-help">250<span class="sig-name descname"><span class="pre">--help</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-trace-help" title="Link to this definition">¶</a></dt>251<dd><p>Display usage and exit.</p>252</dd></dl>253 254<dl class="std option">255<dt class="sig sig-object std" id="cmdoption-trace-version">256<span class="sig-name descname"><span class="pre">--version</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-trace-version" title="Link to this definition">¶</a></dt>257<dd><p>Display the version of the module and exit.</p>258</dd></dl>259 260<div class="versionadded">261<p><span class="versionmodified added">Added in version 3.8: </span>Added <code class="docutils literal notranslate"><span class="pre">--module</span></code> option that allows running an executable module.</p>262</div>263<section id="main-options">264<h3>Main options<a class="headerlink" href="#main-options" title="Link to this heading">¶</a></h3>265<p>At least one of the following options must be specified when invoking266<code class="xref py py-mod docutils literal notranslate"><span class="pre">trace</span></code>. The <a class="reference internal" href="#cmdoption-trace-l"><code class="xref std std-option docutils literal notranslate"><span class="pre">--listfuncs</span></code></a> option is mutually exclusive with267the <a class="reference internal" href="#cmdoption-trace-t"><code class="xref std std-option docutils literal notranslate"><span class="pre">--trace</span></code></a> and <a class="reference internal" href="#cmdoption-trace-c"><code class="xref std std-option docutils literal notranslate"><span class="pre">--count</span></code></a> options. When268<code class="xref std std-option docutils literal notranslate"><span class="pre">--listfuncs</span></code> is provided, neither <code class="xref std std-option docutils literal notranslate"><span class="pre">--count</span></code> nor269<code class="xref std std-option docutils literal notranslate"><span class="pre">--trace</span></code> are accepted, and vice versa.</p>270<dl class="std option">271<dt class="sig sig-object std" id="cmdoption-trace-c">272<span id="cmdoption-trace-count"></span><span class="sig-name descname"><span class="pre">-c</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--count</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-trace-c" title="Link to this definition">¶</a></dt>273<dd><p>Produce a set of annotated listing files upon program completion that shows274how many times each statement was executed. See also275<a class="reference internal" href="#cmdoption-trace-C"><code class="xref std std-option docutils literal notranslate"><span class="pre">--coverdir</span></code></a>, <a class="reference internal" href="#cmdoption-trace-f"><code class="xref std std-option docutils literal notranslate"><span class="pre">--file</span></code></a> and276<a class="reference internal" href="#cmdoption-trace-R"><code class="xref std std-option docutils literal notranslate"><span class="pre">--no-report</span></code></a> below.</p>277</dd></dl>278 279<dl class="std option">280<dt class="sig sig-object std" id="cmdoption-trace-t">281<span id="cmdoption-trace-trace"></span><span class="sig-name descname"><span class="pre">-t</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--trace</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-trace-t" title="Link to this definition">¶</a></dt>282<dd><p>Display lines as they are executed.</p>283</dd></dl>284 285<dl class="std option">286<dt class="sig sig-object std" id="cmdoption-trace-l">287<span id="cmdoption-trace-listfuncs"></span><span class="sig-name descname"><span class="pre">-l</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--listfuncs</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-trace-l" title="Link to this definition">¶</a></dt>288<dd><p>Display the functions executed by running the program.</p>289</dd></dl>290 291<dl class="std option">292<dt class="sig sig-object std" id="cmdoption-trace-r">293<span id="cmdoption-trace-report"></span><span class="sig-name descname"><span class="pre">-r</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--report</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-trace-r" title="Link to this definition">¶</a></dt>294<dd><p>Produce an annotated list from an earlier program run that used the295<a class="reference internal" href="#cmdoption-trace-c"><code class="xref std std-option docutils literal notranslate"><span class="pre">--count</span></code></a> and <a class="reference internal" href="#cmdoption-trace-f"><code class="xref std std-option docutils literal notranslate"><span class="pre">--file</span></code></a> option. This does not296execute any code.</p>297</dd></dl>298 299<dl class="std option">300<dt class="sig sig-object std" id="cmdoption-trace-T">301<span id="cmdoption-trace-trackcalls"></span><span class="sig-name descname"><span class="pre">-T</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--trackcalls</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-trace-T" title="Link to this definition">¶</a></dt>302<dd><p>Display the calling relationships exposed by running the program.</p>303</dd></dl>304 305</section>306<section id="modifiers">307<h3>Modifiers<a class="headerlink" href="#modifiers" title="Link to this heading">¶</a></h3>308<dl class="std option">309<dt class="sig sig-object std" id="cmdoption-trace-f">310<span id="cmdoption-trace-file"></span><span class="sig-name descname"><span class="pre">-f</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--file</span></span><span class="sig-prename descclassname"><span class="pre">=<file></span></span><a class="headerlink" href="#cmdoption-trace-f" title="Link to this definition">¶</a></dt>311<dd><p>Name of a file to accumulate counts over several tracing runs. Should be312used with the <a class="reference internal" href="#cmdoption-trace-c"><code class="xref std std-option docutils literal notranslate"><span class="pre">--count</span></code></a> option.</p>313</dd></dl>314 315<dl class="std option">316<dt class="sig sig-object std" id="cmdoption-trace-C">317<span id="cmdoption-trace-coverdir"></span><span class="sig-name descname"><span class="pre">-C</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--coverdir</span></span><span class="sig-prename descclassname"><span class="pre">=<dir></span></span><a class="headerlink" href="#cmdoption-trace-C" title="Link to this definition">¶</a></dt>318<dd><p>Directory where the report files go. The coverage report for319<code class="docutils literal notranslate"><span class="pre">package.module</span></code> is written to file <code class="file docutils literal notranslate"><em><span class="pre">dir</span></em><span class="pre">/</span><em><span class="pre">package</span></em><span class="pre">/</span><em><span class="pre">module</span></em><span class="pre">.cover</span></code>.</p>320</dd></dl>321 322<dl class="std option">323<dt class="sig sig-object std" id="cmdoption-trace-m">324<span id="cmdoption-trace-missing"></span><span class="sig-name descname"><span class="pre">-m</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--missing</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-trace-m" title="Link to this definition">¶</a></dt>325<dd><p>When generating annotated listings, mark lines which were not executed with326<code class="docutils literal notranslate"><span class="pre">>>>>>></span></code>.</p>327</dd></dl>328 329<dl class="std option">330<dt class="sig sig-object std" id="cmdoption-trace-s">331<span id="cmdoption-trace-summary"></span><span class="sig-name descname"><span class="pre">-s</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--summary</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-trace-s" title="Link to this definition">¶</a></dt>332<dd><p>When using <a class="reference internal" href="#cmdoption-trace-c"><code class="xref std std-option docutils literal notranslate"><span class="pre">--count</span></code></a> or <a class="reference internal" href="#cmdoption-trace-r"><code class="xref std std-option docutils literal notranslate"><span class="pre">--report</span></code></a>, write a brief333summary to stdout for each file processed.</p>334</dd></dl>335 336<dl class="std option">337<dt class="sig sig-object std" id="cmdoption-trace-R">338<span id="cmdoption-trace-no-report"></span><span class="sig-name descname"><span class="pre">-R</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--no-report</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-trace-R" title="Link to this definition">¶</a></dt>339<dd><p>Do not generate annotated listings. This is useful if you intend to make340several runs with <a class="reference internal" href="#cmdoption-trace-c"><code class="xref std std-option docutils literal notranslate"><span class="pre">--count</span></code></a>, and then produce a single set of341annotated listings at the end.</p>342</dd></dl>343 344<dl class="std option">345<dt class="sig sig-object std" id="cmdoption-trace-g">346<span id="cmdoption-trace-timing"></span><span class="sig-name descname"><span class="pre">-g</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--timing</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-trace-g" title="Link to this definition">¶</a></dt>347<dd><p>Prefix each line with the time since the program started. Only used while348tracing.</p>349</dd></dl>350 351</section>352<section id="filters">353<h3>Filters<a class="headerlink" href="#filters" title="Link to this heading">¶</a></h3>354<p>These options may be repeated multiple times.</p>355<dl class="std option">356<dt class="sig sig-object std" id="cmdoption-trace-ignore-module">357<span class="sig-name descname"><span class="pre">--ignore-module</span></span><span class="sig-prename descclassname"><span class="pre">=<mod></span></span><a class="headerlink" href="#cmdoption-trace-ignore-module" title="Link to this definition">¶</a></dt>358<dd><p>Ignore each of the given module names and its submodules (if it is a359package). The argument can be a list of names separated by a comma.</p>360</dd></dl>361 362<dl class="std option">363<dt class="sig sig-object std" id="cmdoption-trace-ignore-dir">364<span class="sig-name descname"><span class="pre">--ignore-dir</span></span><span class="sig-prename descclassname"><span class="pre">=<dir></span></span><a class="headerlink" href="#cmdoption-trace-ignore-dir" title="Link to this definition">¶</a></dt>365<dd><p>Ignore all modules and packages in the named directory and subdirectories.366The argument can be a list of directories separated by <a class="reference internal" href="os.html#os.pathsep" title="os.pathsep"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.pathsep</span></code></a>.</p>367</dd></dl>368 369</section>370</section>371<section id="programmatic-interface">372<span id="trace-api"></span><h2>Programmatic Interface<a class="headerlink" href="#programmatic-interface" title="Link to this heading">¶</a></h2>373<dl class="py class">374<dt class="sig sig-object py" id="trace.Trace">375<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">trace.</span></span><span class="sig-name descname"><span class="pre">Trace</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">count</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">trace</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">countfuncs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">countcallers</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">ignoremods</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">()</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">ignoredirs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">()</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">infile</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">outfile</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">timing</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#trace.Trace" title="Link to this definition">¶</a></dt>376<dd><p>Create an object to trace execution of a single statement or expression. All377parameters are optional. <em>count</em> enables counting of line numbers. <em>trace</em>378enables line execution tracing. <em>countfuncs</em> enables listing of the379functions called during the run. <em>countcallers</em> enables call relationship380tracking. <em>ignoremods</em> is a list of modules or packages to ignore.381<em>ignoredirs</em> is a list of directories whose modules or packages should be382ignored. <em>infile</em> is the name of the file from which to read stored count383information. <em>outfile</em> is the name of the file in which to write updated384count information. <em>timing</em> enables a timestamp relative to when tracing was385started to be displayed.</p>386<dl class="py method">387<dt class="sig sig-object py" id="trace.Trace.run">388<span class="sig-name descname"><span class="pre">run</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">cmd</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#trace.Trace.run" title="Link to this definition">¶</a></dt>389<dd><p>Execute the command and gather statistics from the execution with390the current tracing parameters. <em>cmd</em> must be a string or code object,391suitable for passing into <a class="reference internal" href="functions.html#exec" title="exec"><code class="xref py py-func docutils literal notranslate"><span class="pre">exec()</span></code></a>.</p>392</dd></dl>393 394<dl class="py method">395<dt class="sig sig-object py" id="trace.Trace.runctx">396<span class="sig-name descname"><span class="pre">runctx</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">cmd</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">globals</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">locals</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#trace.Trace.runctx" title="Link to this definition">¶</a></dt>397<dd><p>Execute the command and gather statistics from the execution with the398current tracing parameters, in the defined global and local399environments. If not defined, <em>globals</em> and <em>locals</em> default to empty400dictionaries.</p>401</dd></dl>402 403<dl class="py method">404<dt class="sig sig-object py" id="trace.Trace.runfunc">405<span class="sig-name descname"><span class="pre">runfunc</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func</span></span></em>, <em class="sig-param"><span class="positional-only-separator o"><abbr title="Positional-only parameter separator (PEP 570)"><span class="pre">/</span></abbr></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwds</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#trace.Trace.runfunc" title="Link to this definition">¶</a></dt>406<dd><p>Call <em>func</em> with the given arguments under control of the <code class="xref py py-class docutils literal notranslate"><span class="pre">Trace</span></code>407object with the current tracing parameters.</p>408</dd></dl>409 410<dl class="py method">411<dt class="sig sig-object py" id="trace.Trace.results">412<span class="sig-name descname"><span class="pre">results</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#trace.Trace.results" title="Link to this definition">¶</a></dt>413<dd><p>Return a <a class="reference internal" href="#trace.CoverageResults" title="trace.CoverageResults"><code class="xref py py-class docutils literal notranslate"><span class="pre">CoverageResults</span></code></a> object that contains the cumulative414results of all previous calls to <code class="docutils literal notranslate"><span class="pre">run</span></code>, <code class="docutils literal notranslate"><span class="pre">runctx</span></code> and <code class="docutils literal notranslate"><span class="pre">runfunc</span></code>415for the given <code class="xref py py-class docutils literal notranslate"><span class="pre">Trace</span></code> instance. Does not reset the accumulated416trace results.</p>417</dd></dl>418 419</dd></dl>420 421<dl class="py class">422<dt class="sig sig-object py" id="trace.CoverageResults">423<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">trace.</span></span><span class="sig-name descname"><span class="pre">CoverageResults</span></span><a class="headerlink" href="#trace.CoverageResults" title="Link to this definition">¶</a></dt>424<dd><p>A container for coverage results, created by <a class="reference internal" href="#trace.Trace.results" title="trace.Trace.results"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Trace.results()</span></code></a>. Should425not be created directly by the user.</p>426<dl class="py method">427<dt class="sig sig-object py" id="trace.CoverageResults.update">428<span class="sig-name descname"><span class="pre">update</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">other</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#trace.CoverageResults.update" title="Link to this definition">¶</a></dt>429<dd><p>Merge in data from another <code class="xref py py-class docutils literal notranslate"><span class="pre">CoverageResults</span></code> object.</p>430</dd></dl>431 432<dl class="py method">433<dt class="sig sig-object py" id="trace.CoverageResults.write_results">434<span class="sig-name descname"><span class="pre">write_results</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">show_missing</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">summary</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">coverdir</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">ignore_missing_files</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#trace.CoverageResults.write_results" title="Link to this definition">¶</a></dt>435<dd><p>Write coverage results. Set <em>show_missing</em> to show lines that had no436hits. Set <em>summary</em> to include in the output the coverage summary per437module. <em>coverdir</em> specifies the directory into which the coverage438result files will be output. If <code class="docutils literal notranslate"><span class="pre">None</span></code>, the results for each source439file are placed in its directory.</p>440<p>If <em>ignore_missing_files</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, coverage counts for files that no441longer exist are silently ignored. Otherwise, a missing file will442raise a <a class="reference internal" href="exceptions.html#FileNotFoundError" title="FileNotFoundError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileNotFoundError</span></code></a>.</p>443<div class="versionchanged">444<p><span class="versionmodified changed">Changed in version 3.13: </span>Added <em>ignore_missing_files</em> parameter.</p>445</div>446</dd></dl>447 448</dd></dl>449 450<p>A simple example demonstrating the use of the programmatic interface:</p>451<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">sys</span>452<span class="kn">import</span><span class="w"> </span><span class="nn">trace</span>453 454<span class="c1"># create a Trace object, telling it what to ignore, and whether to</span>455<span class="c1"># do tracing or line-counting or both.</span>456<span class="n">tracer</span> <span class="o">=</span> <span class="n">trace</span><span class="o">.</span><span class="n">Trace</span><span class="p">(</span>457 <span class="n">ignoredirs</span><span class="o">=</span><span class="p">[</span><span class="n">sys</span><span class="o">.</span><span class="n">prefix</span><span class="p">,</span> <span class="n">sys</span><span class="o">.</span><span class="n">exec_prefix</span><span class="p">],</span>458 <span class="n">trace</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>459 <span class="n">count</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>460 461<span class="c1"># run the new command using the given tracer</span>462<span class="n">tracer</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="s1">'main()'</span><span class="p">)</span>463 464<span class="c1"># make a report, placing output in the current directory</span>465<span class="n">r</span> <span class="o">=</span> <span class="n">tracer</span><span class="o">.</span><span class="n">results</span><span class="p">()</span>466<span class="n">r</span><span class="o">.</span><span class="n">write_results</span><span class="p">(</span><span class="n">show_missing</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">coverdir</span><span class="o">=</span><span class="s2">"."</span><span class="p">)</span>467</pre></div>468</div>469</section>470</section>471 472 473 <div class="clearer"></div>474 </div>475 </div>476 </div>477 <div class="sphinxsidebar" role="navigation" aria-label="Main">478 <div class="sphinxsidebarwrapper">479 <div>480 <h3><a href="../contents.html">Table of Contents</a></h3>481 <ul>482<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">trace</span></code> — Trace or track Python statement execution</a><ul>483<li><a class="reference internal" href="#command-line-usage">Command-Line Usage</a><ul>484<li><a class="reference internal" href="#main-options">Main options</a></li>485<li><a class="reference internal" href="#modifiers">Modifiers</a></li>486<li><a class="reference internal" href="#filters">Filters</a></li>487</ul>488</li>489<li><a class="reference internal" href="#programmatic-interface">Programmatic Interface</a></li>490</ul>491</li>492</ul>493 494 </div>495 <div>496 <h4>Previous topic</h4>497 <p class="topless"><a href="timeit.html"498 title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">timeit</span></code> — Measure execution time of small code snippets</a></p>499 </div>500 <div>501 <h4>Next topic</h4>502 <p class="topless"><a href="tracemalloc.html"503 title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tracemalloc</span></code> — Trace memory allocations</a></p>504 </div>505 <script>506 document.addEventListener('DOMContentLoaded', () => {507 const title = document.querySelector('meta[property="og:title"]').content;508 const elements = document.querySelectorAll('.improvepage');509 const pageurl = window.location.href.split('?')[0];510 elements.forEach(element => {511 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));512 url.searchParams.set('pagetitle', title);513 url.searchParams.set('pageurl', pageurl);514 url.searchParams.set('pagesource', "library/trace.rst");515 element.href = url.toString();516 });517 });518 </script>519 <div role="note" aria-label="source link">520 <h3>This page</h3>521 <ul class="this-page-menu">522 <li><a href="../bugs.html">Report a bug</a></li>523 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>524 <li>525 <a href="https://github.com/python/cpython/blob/main/Doc/library/trace.rst?plain=1"526 rel="nofollow">Show source527 </a>528 </li>529 530 </ul>531 </div>532 </div>533<div id="sidebarbutton" title="Collapse sidebar">534<span>«</span>535</div>536 537 </div>538 <div class="clearer"></div>539 </div> 540 <div class="related" role="navigation" aria-label="Related">541 <h3>Navigation</h3>542 <ul>543 <li class="right" style="margin-right: 10px">544 <a href="../genindex.html" title="General Index"545 >index</a></li>546 <li class="right" >547 <a href="../py-modindex.html" title="Python Module Index"548 >modules</a> |</li>549 <li class="right" >550 <a href="tracemalloc.html" title="tracemalloc — Trace memory allocations"551 >next</a> |</li>552 <li class="right" >553 <a href="timeit.html" title="timeit — Measure execution time of small code snippets"554 >previous</a> |</li>555 556 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>557 <li><a href="https://www.python.org/">Python</a> »</li>558 <li class="switchers">559 <div class="language_switcher_placeholder"></div>560 <div class="version_switcher_placeholder"></div>561 </li>562 <li>563 564 </li>565 <li id="cpython-language-and-version">566 <a href="../index.html">3.15.0a6 Documentation</a> »567 </li>568 569 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>570 <li class="nav-item nav-item-2"><a href="debug.html" >Debugging and profiling</a> »</li>571 <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">trace</span></code> — Trace or track Python statement execution</a></li>572 <li class="right">573 574 575 <div class="inline-search" role="search">576 <form class="inline-search" action="../search.html" method="get">577 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">578 <input type="submit" value="Go">579 </form>580 </div>581 |582 </li>583 <li class="right">584<label class="theme-selector-label">585 Theme586 <select class="theme-selector" oninput="activateTheme(this.value)">587 <option value="auto" selected>Auto</option>588 <option value="light">Light</option>589 <option value="dark">Dark</option>590 </select>591</label> |</li>592 593 </ul>594 </div> 595 <div class="footer">596 © <a href="../copyright.html">Copyright</a> 2001 Python Software Foundation.597 <br>598 This page is licensed under the Python Software Foundation License Version 2.599 <br>600 Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.601 <br>602 603 See <a href="/license.html">History and License</a> for more information.<br>604 605 606 <br>607 608 The Python Software Foundation is a non-profit corporation.609<a href="https://www.python.org/psf/donations/">Please donate.</a>610<br>611 <br>612 Last updated on Mar 10, 2026 (08:58 UTC).613 614 <a href="/bugs.html">Found a bug</a>?615 616 <br>617 618 Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.2.3.619 </div>620 621 </body>622</html>