Scripts2026年7月28日·1 分钟阅读

PySnooper — Never Use Print for Python Debugging Again

A debugging tool that lets you trace Python function execution by adding a single decorator. See every line executed, local variable changes, and return values without setting breakpoints.

Agent 就绪

先审查再安装

这个资产需要先审查。复制的指令会要求 Agent dry-run、列出写入项,确认后再继续。

Needs Confirmation · 64/100策略:需确认
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
PySnooper
先审查命令
npx -y tokrepo@latest install 6f5466b0-8aa2-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run,确认写入项后再运行此命令。

Introduction

PySnooper is a poor man's debugger for Python. Instead of painstakingly adding print statements or configuring a full debugger, you decorate a function with @pysnooper.snoop() and get a complete trace of every line executed, every variable modified, and every return value.

What PySnooper Does

  • Traces function execution line by line with a single decorator
  • Logs all local variable assignments and their new values
  • Shows elapsed time for each traced line
  • Supports writing trace output to files or custom streams
  • Allows watching specific expressions and nested function calls

Architecture Overview

PySnooper hooks into Python's sys.settrace mechanism. When a decorated function runs, the tracer intercepts each line event, inspects the local namespace for changes, formats the output, and writes it to stderr or a specified file. The @pysnooper.snoop() decorator wraps the target function transparently, so no changes to the function body are needed.

Self-Hosting & Configuration

  • Install with pip: pip install pysnooper
  • Apply @pysnooper.snoop() to any function you want to trace
  • Write traces to a file: @pysnooper.snoop("/tmp/debug.log")
  • Watch specific expressions: @pysnooper.snoop(watch=("len(items)", "self.count"))
  • Trace deeper calls: @pysnooper.snoop(depth=2) to follow nested function invocations

Key Features

  • Zero-config debugging with a single decorator
  • Traces variable changes without manual print statements
  • Supports conditional tracing with watch and watch_explode
  • Thread-safe with per-thread trace output
  • Lightweight with no dependencies beyond the Python standard library

Comparison with Similar Tools

  • print() — Requires manual placement and removal; PySnooper automates the entire trace
  • pdb / breakpoint() — Interactive debugger requiring stepping; PySnooper is non-interactive and logs everything
  • icecream (ic) — Focused on inspect-style printing; PySnooper traces full execution flow
  • Hunter — More powerful tracing framework; PySnooper is simpler for quick debugging

FAQ

Q: Does PySnooper slow down my code? A: Yes, tracing adds overhead. Use it only during development and debugging, not in production. Remove the decorator when done.

Q: Can I use PySnooper with async functions? A: PySnooper works with synchronous functions. For async code, consider alternatives like snoop (a fork) or logging-based approaches.

Q: How do I trace only certain variables? A: Use the watch parameter: @pysnooper.snoop(watch=("my_var",)) to track specific expressions alongside the default local variable tracing.

Q: Can I integrate PySnooper with logging? A: Yes. Pass a logging handler or file object as the output target: @pysnooper.snoop(output=my_logger).

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产