Top Related Projects
Quick Overview
PySnooper is a Python debugging tool that helps developers understand code execution by generating detailed logs. It allows you to trace the execution of your Python code, showing variable values, function calls, and other relevant information without manually adding print statements.
Pros
- Easy to use with minimal setup required
- Provides detailed execution logs, including variable values and function calls
- Can be used as a decorator or context manager for flexible implementation
- Supports customization of output and logging behavior
Cons
- May generate large log files for complex or long-running programs
- Can slow down code execution, especially when tracing many variables
- Limited to tracing Python code and not suitable for debugging compiled extensions
- May require additional configuration for optimal use in production environments
Code Examples
- Basic usage as a decorator:
import pysnooper
@pysnooper.snoop()
def calculate_factorial(n):
if n == 0:
return 1
else:
return n * calculate_factorial(n - 1)
result = calculate_factorial(5)
print(f"Factorial of 5 is: {result}")
- Using PySnooper as a context manager:
import pysnooper
def process_data(data):
with pysnooper.snoop():
result = []
for item in data:
processed = item * 2
result.append(processed)
return result
data = [1, 2, 3, 4, 5]
processed_data = process_data(data)
print(f"Processed data: {processed_data}")
- Customizing PySnooper output:
import pysnooper
@pysnooper.snoop(output='debug.log', variables=('x', 'y'), depth=2)
def complex_calculation(a, b):
x = a * 2
y = b + 3
return x ** y
result = complex_calculation(3, 4)
print(f"Result of complex calculation: {result}")
Getting Started
To use PySnooper, first install it using pip:
pip install pysnooper
Then, import and use it in your Python code:
import pysnooper
@pysnooper.snoop()
def your_function():
# Your code here
pass
# Or use it as a context manager
with pysnooper.snoop():
# Your code here
pass
Customize the output and behavior using various parameters like output
, variables
, depth
, and more. Refer to the PySnooper documentation for advanced usage and configuration options.
Competitor Comparisons
A powerful set of Python debugging tools, based on PySnooper
Pros of snoop
- More advanced features like custom formatters and watch expressions
- Better handling of asynchronous code and generators
- Supports tracing function calls and return values
Cons of snoop
- Slightly more complex API, which may be overwhelming for beginners
- Potentially higher performance overhead due to additional features
Code Comparison
snoop:
import snoop
@snoop
def my_function(x, y):
z = x + y
return z * 2
PySnooper:
import pysnooper
@pysnooper.snoop()
def my_function(x, y):
z = x + y
return z * 2
Both libraries offer similar basic functionality for debugging Python code. PySnooper focuses on simplicity and ease of use, making it ideal for quick debugging tasks. snoop provides more advanced features and customization options, which can be beneficial for complex debugging scenarios but may require a steeper learning curve.
While the basic usage is similar, snoop offers additional capabilities like custom formatters, watch expressions, and better support for asynchronous code. However, this comes at the cost of a slightly more complex API and potentially higher performance overhead.
Choose PySnooper for quick and simple debugging tasks, and consider snoop for more advanced debugging needs or when working with complex codebases.
Full-screen console debugger for Python
Pros of pudb
- Full-featured console-based visual debugger with a text user interface
- Provides a comprehensive view of variables, stack, and breakpoints
- Allows for interactive debugging with step-by-step execution
Cons of pudb
- Requires manual setup and invocation of the debugger
- May have a steeper learning curve for users new to visual debuggers
- Limited to console-based environments, not suitable for GUI applications
Code Comparison
PySnooper usage:
import pysnooper
@pysnooper.snoop()
def my_function(x, y):
z = x + y
return z
pudb usage:
import pudb
pudb.set_trace()
def my_function(x, y):
z = x + y
return z
Key Differences
PySnooper is a lightweight, decorator-based debugging tool that automatically logs variable changes and function calls. It's easy to use and requires minimal setup.
pudb is a more comprehensive debugger with a text-based user interface, offering features like breakpoints, variable inspection, and step-by-step execution. It provides a more traditional debugging experience but requires manual invocation.
Both tools serve different debugging needs and can be valuable in different scenarios, depending on the complexity of the debugging task and user preferences.
Integration of IPython pdb
Pros of ipdb
- Interactive debugging with a command-line interface
- Supports all features of the Python debugger (pdb)
- Allows for stepping through code, setting breakpoints, and inspecting variables
Cons of ipdb
- Requires manual insertion of breakpoints or running the entire script in debug mode
- May be more complex for beginners compared to PySnooper's automatic tracing
- Doesn't provide automatic logging of variable changes and function calls
Code Comparison
ipdb:
import ipdb
def my_function():
x = 5
ipdb.set_trace()
y = x + 10
return y
PySnooper:
import pysnooper
@pysnooper.snoop()
def my_function():
x = 5
y = x + 10
return y
PySnooper automatically traces the function execution and logs variable changes, while ipdb requires manual insertion of breakpoints and interactive debugging.
An implementation of the Debug Adapter Protocol for Python
Pros of debugpy
- Full-featured debugger with remote debugging capabilities
- Integrates well with IDEs and development environments
- Supports advanced features like conditional breakpoints and variable watching
Cons of debugpy
- More complex setup and usage compared to PySnooper
- Heavier resource usage, which may impact performance in some scenarios
Code Comparison
PySnooper usage:
import pysnooper
@pysnooper.snoop()
def my_function(x, y):
z = x + y
return z
debugpy usage:
import debugpy
debugpy.listen(5678)
debugpy.wait_for_client()
debugpy.breakpoint()
def my_function(x, y):
z = x + y
return z
Summary
PySnooper is a lightweight, easy-to-use debugging tool that focuses on tracing function execution and variable changes. It's ideal for quick debugging tasks and requires minimal setup.
debugpy, on the other hand, is a comprehensive debugging solution that offers more advanced features and better integration with development environments. It's more suitable for complex debugging scenarios and larger projects but requires more setup and resources.
The choice between the two depends on the specific debugging needs, project complexity, and developer preferences. PySnooper is great for quick insights, while debugpy provides a more robust debugging experience.
pdb++, a drop-in replacement for pdb (the Python debugger)
Pros of pdbpp
- More feature-rich debugger with advanced functionality like sticky mode and syntax highlighting
- Integrates seamlessly with Python's built-in pdb, enhancing the familiar debugging experience
- Offers a command-line interface for interactive debugging sessions
Cons of pdbpp
- Requires installation and setup, unlike PySnooper which can be used with a simple decorator
- May have a steeper learning curve due to its extensive feature set
- Less suitable for quick, automated debugging of specific functions or code blocks
Code Comparison
pdbpp usage:
import pdb
pdb.set_trace()
# Your code here
PySnooper usage:
import pysnooper
@pysnooper.snoop()
def your_function():
# Your code here
While pdbpp enhances the interactive debugging experience, PySnooper focuses on automated tracing and logging of function execution. pdbpp is more suitable for in-depth, interactive debugging sessions, whereas PySnooper excels at quick, non-intrusive debugging of specific functions or code blocks.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
PySnooper - Never use print for debugging again
PySnooper is a poor man's debugger. If you've used Bash, it's like set -x
for Python, except it's fancier.
Your story: You're trying to figure out why your Python code isn't doing what you think it should be doing. You'd love to use a full-fledged debugger with breakpoints and watches, but you can't be bothered to set one up right now.
You want to know which lines are running and which aren't, and what the values of the local variables are.
Most people would use print
lines, in strategic locations, some of them showing the values of variables.
PySnooper lets you do the same, except instead of carefully crafting the right print
lines, you just add one decorator line to the function you're interested in. You'll get a play-by-play log of your function, including which lines ran and when, and exactly when local variables were changed.
What makes PySnooper stand out from all other code intelligence tools? You can use it in your shitty, sprawling enterprise codebase without having to do any setup. Just slap the decorator on, as shown below, and redirect the output to a dedicated log file by specifying its path as the first argument.
Example
We're writing a function that converts a number to binary, by returning a list of bits. Let's snoop on it by adding the @pysnooper.snoop()
decorator:
import pysnooper
@pysnooper.snoop()
def number_to_bits(number):
if number:
bits = []
while number:
number, remainder = divmod(number, 2)
bits.insert(0, remainder)
return bits
else:
return [0]
number_to_bits(6)
The output to stderr is:
Or if you don't want to trace an entire function, you can wrap the relevant part in a with
block:
import pysnooper
import random
def foo():
lst = []
for i in range(10):
lst.append(random.randrange(1, 1000))
with pysnooper.snoop():
lower = min(lst)
upper = max(lst)
mid = (lower + upper) / 2
print(lower, mid, upper)
foo()
which outputs something like:
New var:....... i = 9
New var:....... lst = [681, 267, 74, 832, 284, 678, ...]
09:37:35.881721 line 10 lower = min(lst)
New var:....... lower = 74
09:37:35.882137 line 11 upper = max(lst)
New var:....... upper = 832
09:37:35.882304 line 12 mid = (lower + upper) / 2
74 453.0 832
New var:....... mid = 453.0
09:37:35.882486 line 13 print(lower, mid, upper)
Elapsed time: 00:00:00.000344
Features
If stderr is not easily accessible for you, you can redirect the output to a file:
@pysnooper.snoop('/my/log/file.log')
You can also pass a stream or a callable instead, and they'll be used.
See values of some expressions that aren't local variables:
@pysnooper.snoop(watch=('foo.bar', 'self.x["whatever"]'))
Show snoop lines for functions that your function calls:
@pysnooper.snoop(depth=2)
See Advanced Usage for more options. <------
Installation with Pip
The best way to install PySnooper is with Pip:
$ pip install pysnooper
Other installation options
Conda with conda-forge channel:
$ conda install -c conda-forge pysnooper
Arch Linux:
$ yay -S python-pysnooper
Fedora Linux:
$ dnf install python3-pysnooper
Citing PySnooper
If you use PySnooper in academic work, please use this citation format:
@software{rachum2019pysnooper,
title={PySnooper: Never use print for debugging again},
author={Rachum, Ram and Hall, Alex and Yanokura, Iori and others},
year={2019},
month={jun},
publisher={PyCon Israel},
doi={10.5281/zenodo.10462459},
url={https://github.com/cool-RR/PySnooper}
}
License
Copyright (c) 2019 Ram Rachum and collaborators, released under the MIT license.
Media Coverage
Hacker News thread and /r/Python Reddit thread (22 April 2019)
Top Related Projects
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot