Skip to content

PySnooper

A Python decorator that logs which lines run and how local variables change, instead of print statements.

Stars
16.6k
Forks
972
Licence
MIT
Language
Python
Last update
5 months ago
Type
Library

What it is

PySnooper is a small Python library for debugging. You add a single decorator line to a function, and it prints a step-by-step log showing which lines ran, when they ran, and when local variables changed. It can also wrap just part of a function in a with block, and the output can go to stderr or a log file.

  • Add @pysnooper.snoop() to a function to see its line-by-line execution and variable changes
  • Wrap a section of code in a with pysnooper.snoop(): block to trace only that part
  • Send the trace to a log file by passing a path to snoop()
  • Use depth=2 to also trace functions that the decorated function calls

Great for

  • Developers who want quick insight into a function without setting up a full debugger
  • People debugging code in a large existing codebase where setup is inconvenient
  • Beginners who are used to print-based debugging and want a more structured trace
  • Anyone wanting to see values of expressions like attributes or dict lookups via the watch option

Not ideal for

  • People who need breakpoints, stepping, or interactive inspection; PySnooper only produces a log
  • Teams debugging performance-sensitive code, since the README does not describe its overhead and tracing adds output
  • Non-Python projects, since it only works with Python code
Step-by-step guide: how to use PySnooperInstall, first run, examples, tips and FAQs

How to get it

  1. 1

    Install with a package manager

    You'll need: Python

    Install with pip

    pip install pysnooper
  2. 2

    Run it

    • Install the package with pip install pysnooper
    • Import pysnooper in your script
    • Add @pysnooper.snoop() above the function you want to trace
    • Run the script and read the trace on stderr
  3. 3

    Get the most out of it

    • Pass a file path to @pysnooper.snoop() to keep traces out of the terminal
    • Use watch=(...) to log values of expressions that are not local variables
    • Use depth=2 to include traces from functions called by your function
    • See ADVANCED_USAGE.md in the repository for further options

Downloads

Strengths

  • Requires only one decorator line, with no separate debugger setup
  • Shows when each local variable changes, not just its value at one moment
  • Output can be redirected to a file, stream, or callable

Trade-offs

  • It is a logging tool, so it cannot pause execution or step through code interactively
  • The README is brief about performance impact and limitations, so details are in ADVANCED_USAGE.md which was not provided
  • Output can be verbose for large functions

How it compares

ProjectStarsTypeLevelUpdatedBest for
PySnooper (this)16.6kLibrarybeginner5 months agoDevelopers who want quick insight into a function without setting up a full debugger
icecream10.1kLibrarybeginner17 days agoPython developers who add print() statements to debug code
debug11.5kLibrarybeginner23 days agoNode.js developers who want named, toggleable log output instead of plain console.log calls
logrus25.8kLibrarybeginner1 months agoGo developers with existing code that already uses Logrus
zap24.7kLibrarybeginner2 months agoGo developers building services that log heavily
loguru24.2kLibrarybeginner12 days agoPython developers who want logging without much setup

Stats synced from GitHub. The assessment above was drafted with AI from the project's README on 2026-10-29 and may miss recent changes — check the README before relying on a command. MIT licence: free to use, modify and distribute, including commercially, as long as you keep the copyright notice.