TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Tracing Python

104 pointsby jimylalmost 2 years ago

7 comments

peterkellyalmost 2 years ago
Python has a built-in API for tracing: <a href="https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;sys.html#sys.settrace" rel="nofollow">https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;sys.html#sys.settrace</a><p>Here&#x27;s an example of how to use it (obviously a minimal case, you can customise your handler do do whatever you need):<p><pre><code> def mytrace(frame, event, arg): if event == &quot;call&quot;: print(&quot;call&quot;, frame.f_code.co_name, frame.f_locals) elif event == &quot;return&quot;: print(&quot;return&quot;, frame.f_code.co_name, arg) return mytrace import sys sys.settrace(mytrace) </code></pre> For example, given the following code:<p><pre><code> def three(z): return 3 def two(y): return three(&quot;BBB&quot;) - 1 def one(): return two(&quot;AAA&quot;) - 1 one() </code></pre> It will output:<p><pre><code> call one {} call two {&#x27;y&#x27;: &#x27;AAA&#x27;} call three {&#x27;z&#x27;: &#x27;BBB&#x27;} return three 3 return two 2 return one 1</code></pre>
评论 #36066878 未加载
评论 #36067422 未加载
评论 #36067520 未加载
lathiatalmost 2 years ago
There are many different tools, but when I want to quickly understand what the code was doing I have been reaching for pysnooper: <a href="https:&#x2F;&#x2F;pypi.org&#x2F;project&#x2F;PySnooper&#x2F;" rel="nofollow">https:&#x2F;&#x2F;pypi.org&#x2F;project&#x2F;PySnooper&#x2F;</a><p>Has a pretty nice and importantly colored output by default, which I find much easier to follow. In many cases snooping one specific function has done the job. It has an easy to use decorator to decorate any function without having to otherwise intercept the main python invocation or set something up at the start. Good for libraries etc.<p>pysnooper has mostly been enough for me but the &quot;snoop&quot; mentioned in the article seems to be a superset and more. Will have to give it a go next time.<p>Outside of this if I want to understand performance or more roughly what code paths are executing (and not a per-statement trace) then austin and py-spy are both amazing.. convert to flamescope and drop into flamescope.app.
sys_64738almost 2 years ago
A more traditional debugger for Python.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;inducer&#x2F;pudb">https:&#x2F;&#x2F;github.com&#x2F;inducer&#x2F;pudb</a>
knlb2022almost 2 years ago
I&#x27;ll throw in my own too: <a href="https:&#x2F;&#x2F;github.com&#x2F;kunalb&#x2F;panopticon">https:&#x2F;&#x2F;github.com&#x2F;kunalb&#x2F;panopticon</a>
Spivakalmost 2 years ago
Fantastic article and something I&#x27;ve been looking for better tools for. None of them seem to have my ideal workflow though -- providing an external manifest of what and how to trace that can be used against an existing program without changing the code. Something like Otel&#x27;s auto instrumentation.
chrisshrobaalmost 2 years ago
Fyi, the table of contents gets cut off (its box is smaller than its contents) at zoom levels of 90% or larger for me. Here&#x27;s a screenshot at 100% zoom <a href="https:&#x2F;&#x2F;imgur.com&#x2F;a&#x2F;0t9cKs5" rel="nofollow">https:&#x2F;&#x2F;imgur.com&#x2F;a&#x2F;0t9cKs5</a><p>Great article!
nomelalmost 2 years ago
What&#x27;s the modern practical use case for tracing in python? Environments without an IDE&#x2F;remote debugger?<p>I can&#x27;t recall the last time I needed to enable tracing, in the last couple of decades, besides bash scripts.
评论 #36068054 未加载