Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cool-RR/PySnooper
Never use print for debugging again
https://github.com/cool-RR/PySnooper
debug debugger introspection logging python
Last synced: about 2 months ago
JSON representation
Never use print for debugging again
- Host: GitHub
- URL: https://github.com/cool-RR/PySnooper
- Owner: cool-RR
- License: mit
- Created: 2019-04-18T13:55:43.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-09-09T07:41:37.000Z (3 months ago)
- Last Synced: 2024-10-13T13:45:31.253Z (about 2 months ago)
- Topics: debug, debugger, introspection, logging, python
- Language: Python
- Homepage:
- Size: 482 KB
- Stars: 16,369
- Watchers: 228
- Forks: 952
- Open Issues: 29
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Authors: AUTHORS
Awesome Lists containing this project
- awesome - PySnooper - Never use print for debugging again (Python)
- awesome - PySnooper
- my-awesome-github-stars - cool-RR/PySnooper - Never use print for debugging again (Python)
- awesome-opensource-israel - PySnooper - a Python debugging aid. "Never use print for debugging again." ![GitHub last commit](https://img.shields.io/github/last-commit/cool-RR/PySnooper?style=flat-square) ![GitHub top language](https://img.shields.io/github/languages/top/cool-RR/PySnooper?style=flat-square) (Projects by main language / python)
- awesome-starts - cool-RR/PySnooper - Never use print for debugging again (Python)
- awesome-list - PySnooper - Never use print for debugging again. (Debugging & Profiling & Tracing / For Python)
- best-of-python-dev - GitHub - 20% open · ⏱️ 13.01.2024): (Debugging Tools)
- StarryDivineSky - cool-RR/PySnooper - x ,只是它更高级。 (Python 程序 / 网络服务_其他)
README
# 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:
```python
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:![](https://i.imgur.com/TrF3VVj.jpg)
Or if you don't want to trace an entire function, you can wrap the relevant part in a `with` block:
```python
import pysnooper
import randomdef 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:
```python
@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:
```python
@pysnooper.snoop(watch=('foo.bar', 'self.x["whatever"]'))
```Show snoop lines for functions that your function calls:
```python
@pysnooper.snoop(depth=2)
```**See [Advanced Usage](https://github.com/cool-RR/PySnooper/blob/master/ADVANCED_USAGE.md) for more options.** <------
## Installation with Pip
The best way to install **PySnooper** is with Pip:
```console
$ pip install pysnooper
```## Other installation options
Conda with conda-forge channel:
```console
$ conda install -c conda-forge pysnooper
```Arch Linux:
```console
$ yay -S python-pysnooper
```Fedora Linux:
```console
$ dnf install python3-pysnooper
```## Citing PySnooper
If you use PySnooper in academic work, please use this citation format:
```bibtex
@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](https://news.ycombinator.com/item?id=19717786)
and [/r/Python Reddit thread](https://www.reddit.com/r/Python/comments/bg0ida/pysnooper_never_use_print_for_debugging_again/) (22 April 2019)