PySnooper

1.2.3 · active · verified Mon Apr 13

PySnooper is a Python debugging tool often described as a 'poor man's debugger'. It allows developers to trace the execution of their code by automatically logging variable values, function calls, and execution lines to stdout or a file, without requiring traditional breakpoints or extensive manual print statements. The current stable version is 1.2.3. Recent GitHub activity indicates ongoing maintenance, including support for newer Python versions.

Warnings

Install

Imports

Quickstart

The primary way to use PySnooper is by applying the `@pysnooper.snoop()` decorator to a function you wish to inspect. This will log a detailed trace of execution, including variable changes and lines run. You can redirect output to a file and disable colored output for better readability in text editors. Alternatively, `pysnooper.snoop()` can be used as a context manager for specific code blocks.

import pysnooper
import os

# To demonstrate file output, create a dummy log file path
log_file_path = os.path.join(os.getcwd(), 'pysnooper_output.log')

@pysnooper.snoop(output=log_file_path, color=False)
def number_to_bits(number):
    if number == 0:
        return [0]
    bits = []
    while number:
        number, remainder = divmod(number, 2)
        bits.insert(0, remainder)
    return bits

result = number_to_bits(6)
print(f"Result for 6: {result}")

# You can also use it as a context manager for a block of code
def main_logic():
    a = 10
    b = 20
    with pysnooper.snoop(output=log_file_path, color=False):
        temp = a + b
        final = temp * 2
    print(f"Final value in main_logic: {final}")

main_logic()

# Cleanup (optional, for a runnable example)
# import os
# if os.path.exists(log_file_path):
#     os.remove(log_file_path)

view raw JSON →