PySnooper


NamePySnooper JSON
Version 1.2.0 PyPI version JSON
download
home_pagehttps://github.com/cool-RR/PySnooper
SummaryA poor man's debugger for Python.
upload_time2023-07-15 15:04:20
maintainer
docs_urlNone
authorRam Rachum
requires_python
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            # 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 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:

```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
```

# 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)

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/cool-RR/PySnooper",
    "name": "PySnooper",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Ram Rachum",
    "author_email": "ram@rachum.com",
    "download_url": "https://files.pythonhosted.org/packages/66/08/474e7891ff7107328404b46f42cdc9bf58b99404924d35933037cd0fb145/PySnooper-1.2.0.tar.gz",
    "platform": null,
    "description": "# PySnooper - Never use print for debugging again #\r\n\r\n**PySnooper** is a poor man's debugger. If you've used Bash, it's like `set -x` for Python, except it's fancier.\r\n\r\nYour 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.\r\n\r\nYou want to know which lines are running and which aren't, and what the values of the local variables are.\r\n\r\nMost people would use `print` lines, in strategic locations, some of them showing the values of variables.\r\n\r\n**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.\r\n\r\nWhat 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.\r\n\r\n# Example #\r\n\r\nWe'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:\r\n\r\n```python\r\nimport pysnooper\r\n\r\n@pysnooper.snoop()\r\ndef number_to_bits(number):\r\n    if number:\r\n        bits = []\r\n        while number:\r\n            number, remainder = divmod(number, 2)\r\n            bits.insert(0, remainder)\r\n        return bits\r\n    else:\r\n        return [0]\r\n\r\nnumber_to_bits(6)\r\n```\r\nThe output to stderr is:\r\n\r\n![](https://i.imgur.com/TrF3VVj.jpg)\r\n\r\nOr if you don't want to trace an entire function, you can wrap the relevant part in a `with` block:\r\n\r\n```python\r\nimport pysnooper\r\nimport random\r\n\r\ndef foo():\r\n    lst = []\r\n    for i in range(10):\r\n        lst.append(random.randrange(1, 1000))\r\n\r\n    with pysnooper.snoop():\r\n        lower = min(lst)\r\n        upper = max(lst)\r\n        mid = (lower + upper) / 2\r\n        print(lower, mid, upper)\r\n\r\nfoo()\r\n```\r\n\r\nwhich outputs something like:\r\n\r\n```\r\nNew var:....... i = 9\r\nNew var:....... lst = [681, 267, 74, 832, 284, 678, ...]\r\n09:37:35.881721 line        10         lower = min(lst)\r\nNew var:....... lower = 74\r\n09:37:35.882137 line        11         upper = max(lst)\r\nNew var:....... upper = 832\r\n09:37:35.882304 line        12         mid = (lower + upper) / 2\r\n74 453.0 832\r\nNew var:....... mid = 453.0\r\n09:37:35.882486 line        13         print(lower, mid, upper)\r\nElapsed time: 00:00:00.000344\r\n```\r\n\r\n# Features #\r\n\r\nIf stderr is not easily accessible for you, you can redirect the output to a file:\r\n\r\n```python\r\n@pysnooper.snoop('/my/log/file.log')\r\n```\r\n\r\nYou can also pass a stream or a callable instead, and they'll be used.\r\n\r\nSee values of some expressions that aren't local variables:\r\n\r\n```python\r\n@pysnooper.snoop(watch=('foo.bar', 'self.x[\"whatever\"]'))\r\n```\r\n\r\nShow snoop lines for functions that your function calls:\r\n\r\n```python\r\n@pysnooper.snoop(depth=2)\r\n```\r\n\r\n**See [Advanced Usage](https://github.com/cool-RR/PySnooper/blob/master/ADVANCED_USAGE.md) for more options.** <------\r\n\r\n\r\n# Installation with Pip #\r\n\r\nThe best way to install **PySnooper** is with Pip:\r\n\r\n```console\r\n$ pip install pysnooper\r\n```\r\n\r\n# Other installation options #\r\n\r\nConda with conda-forge channel:\r\n\r\n```console\r\n$ conda install -c conda-forge pysnooper\r\n```\r\n\r\nArch Linux:\r\n\r\n```console\r\n$ yay -S python-pysnooper\r\n```\r\n\r\nFedora Linux:\r\n\r\n```console\r\n$ dnf install python3-pysnooper\r\n```\r\n\r\n# License #\r\n\r\nCopyright (c) 2019 Ram Rachum and collaborators, released under the MIT license.\r\n\r\n\r\n# Media Coverage #\r\n\r\n[Hacker News thread](https://news.ycombinator.com/item?id=19717786)\r\nand [/r/Python Reddit thread](https://www.reddit.com/r/Python/comments/bg0ida/pysnooper_never_use_print_for_debugging_again/) (22 April 2019)\r\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "A poor man's debugger for Python.",
    "version": "1.2.0",
    "project_urls": {
        "Homepage": "https://github.com/cool-RR/PySnooper"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a7b1a5c75d691ac84a5503e69eac5de97523772c787ba84c3750f6ebb713a36f",
                "md5": "5a05896f1746f7539bf6fc136738fe57",
                "sha256": "aa859aa9a746cffc1f35e4ee469d49c3cc5185b5fc0c571feb3af3c94d2eb625"
            },
            "downloads": -1,
            "filename": "PySnooper-1.2.0-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5a05896f1746f7539bf6fc136738fe57",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": null,
            "size": 14818,
            "upload_time": "2023-07-15T15:04:18",
            "upload_time_iso_8601": "2023-07-15T15:04:18.099514Z",
            "url": "https://files.pythonhosted.org/packages/a7/b1/a5c75d691ac84a5503e69eac5de97523772c787ba84c3750f6ebb713a36f/PySnooper-1.2.0-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6608474e7891ff7107328404b46f42cdc9bf58b99404924d35933037cd0fb145",
                "md5": "9b2fe060f46b0c3054e0c30b38cc313a",
                "sha256": "810669e162a250a066d8662e573adbc5af770e937c5b5578f28bb7355d1c859b"
            },
            "downloads": -1,
            "filename": "PySnooper-1.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "9b2fe060f46b0c3054e0c30b38cc313a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 46224,
            "upload_time": "2023-07-15T15:04:20",
            "upload_time_iso_8601": "2023-07-15T15:04:20.223575Z",
            "url": "https://files.pythonhosted.org/packages/66/08/474e7891ff7107328404b46f42cdc9bf58b99404924d35933037cd0fb145/PySnooper-1.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-07-15 15:04:20",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "cool-RR",
    "github_project": "PySnooper",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "tox": true,
    "lcname": "pysnooper"
}
        
Elapsed time: 0.09019s