A poor man's debugger for Python.
PySnooper - Never use print for debugging again
PySnooper is a poor man's debugger.
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
PySnooper lets you do the same, except instead of carefully crafting the right
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.
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
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  number_to_bits(6)
The output to stderr is:
Starting var:.. number = 6 15:29:11.327032 call 4 def number_to_bits(number): 15:29:11.327032 line 5 if number: 15:29:11.327032 line 6 bits =  New var:....... bits =  15:29:11.327032 line 7 while number: 15:29:11.327032 line 8 number, remainder = divmod(number, 2) New var:....... remainder = 0 Modified var:.. number = 3 15:29:11.327032 line 9 bits.insert(0, remainder) Modified var:.. bits =  15:29:11.327032 line 7 while number: 15:29:11.327032 line 8 number, remainder = divmod(number, 2) Modified var:.. number = 1 Modified var:.. remainder = 1 15:29:11.327032 line 9 bits.insert(0, remainder) Modified var:.. bits = [1, 0] 15:29:11.327032 line 7 while number: 15:29:11.327032 line 8 number, remainder = divmod(number, 2) Modified var:.. number = 0 15:29:11.327032 line 9 bits.insert(0, remainder) Modified var:.. bits = [1, 1, 0] 15:29:11.327032 line 7 while number: 15:29:11.327032 line 10 return bits 15:29:11.327032 return 10 return bits Return value:.. [1, 1, 0]
If stderr is not easily accessible for you, you can redirect the output to a file:
See values of some variables that aren't local variables:
Show snoop lines for functions that your function calls:
Start all snoop lines with a prefix, to grep for them easily:
$ pip install pysnooper
Tox installs all dependencies automatically. You only need to install Tox itself:
$ pip install tox
List all environments
tox would run:
$ tox -lv
If you want to run tests agains all target Python versions use pyenv to install them. Otherwise, you can run only linters and the ones you have already installed on your machine:
# run only some environments $ tox -e flake8,pylint,bandit,py27,py36
Linters and tests should pass before you push your code. They will be run again on Travis CI.
Copyright (c) 2019 Ram Rachum and collaborators, released under the MIT license.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size PySnooper_g-0.1.0-py2.py3-none-any.whl (23.8 kB)||File type Wheel||Python version py2.py3||Upload date||Hashes View hashes|
|Filename, size PySnooper-g-0.1.0.tar.gz (20.6 kB)||File type Source||Python version None||Upload date||Hashes View hashes|
Hashes for PySnooper_g-0.1.0-py2.py3-none-any.whl