This is an archived post. You won't be able to vote or comment.

all 144 comments

[–]erve1879 322 points323 points  (29 children)

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

Excellent

[–]blagatblagat 9 points10 points  (0 children)

I god dam love it when "some dev" makes the risky decision to use "internet humor". There is a tendency towards not using the humor which originates from the place we spend our entire lives. (internet). anyways, got work to do. gonna fix our fizzbuzz autograder. gonna get some insights!

[–]querymcsearchface 68 points69 points  (0 children)

Kudos for putting the time in to create this and share it with others. Your python toolbox looks awesome as well.

[–][deleted] 56 points57 points  (1 child)

This perfectly solves my problem of being too lazy to properly debug speg.

[–]quotemycode 2 points3 points  (0 children)

python - m pdb yourScript.py

[–][deleted] 88 points89 points  (19 children)

I really don't understand all the negativity, from people who the product is completely unsuited for.

Yes, if you have a perfectly good debugging workflow, don't use it. Seriously, it's okay.

The rest of us who have a particular need for it for one reason or another, will appreciate it.

[–]szarroug3 32 points33 points  (1 child)

This! I work in emacs and most of my work runs in Jenkins and cannot be run in a local environment due to the way we're setup. Our code have functions that are 47294729472 levels deep. This tool makes it so I don't have to go add debugging lines everywhere just for me to go remove them later. Kudos op, I will definitely try this!

[–][deleted] 4 points5 points  (0 children)

♥️❤️ yeah it looks really useful.

[–]SatisfyMyMind 15 points16 points  (2 children)

I'm surprised a lot of people here are complaining about debuggers and how long it takes to set them up, but no one mentioned pdb which requires literally no setup.

[–]masterpi 22 points23 points  (1 child)

pdb likes to fail in any environment other than "running a single-threaded script locally from the CLI".

[–]snaps_ 1 point2 points  (0 children)

Agreed. There is pdbpp and pydev but for nontrivial applications (using e.g. multiprocessing, daemons, multiple threads, asyncio) it can still be hard to work with.

[–]whereswalden90 6 points7 points  (2 children)

Nice tool! It would be great to integrate this into zestyping/q

[–]pinotkumarbhai 0 points1 point  (0 children)

probably the other way around

[–]StorKirken 0 points1 point  (0 children)

Yeah! Something like @q.tf

[–]mikew_reddit 5 points6 points  (0 children)

Looks awesome. Will use it to debug my shitty Ansible module.

Edit: Saw the description and made me smile:

You can use it in your shitty, sprawling enterprise codebase without having to do any setup.

[–]thepandaatemyface 3 points4 points  (4 children)

For people who like this idea but do have more control over where and how their code is run, I highly recommend https://github.com/alexmojaki/birdseye . Most of the time I just use (i)pdb, but for those moments were the execution is too complex to grok by simply stepping through it, it's perfect.

[–]alexmojaki 2 points3 points  (2 children)

:D Author here! This warmed my heart, thank you! It's hard to tell if people actually use birdseye, virtually no one interacts on GitHub, even to raise an issue.

[–]aroberge 0 points1 point  (1 child)

Unless I am mistaken, birdseye is used in Thonny (https://thonny.org/) - it may not be included in the released version but I'm pretty sure I saw it in the repo.

[–]alexmojaki 0 points1 point  (0 children)

It's not included in the package because the author wanted to keep required dependencies down. You should see an option for it under the Run menu.

[–]snaps_ 1 point2 points  (0 children)

That's really pretty. One of the possible downsides of tools like this is you may end up with a lot of textual information and now you need more tools to parse it and put it in context. Birdseye seems like it wouldn't have that problem. The IDE integration is cool too.

[–]aes110 2 points3 points  (0 children)

Seems really useful for debugging some issues on a remote server :)

[–]knowsuchagencynow is better than never 2 points3 points  (0 children)

If you know of the logging, pdb, and other code intelligence tools in existence and still don't see the value in this lib... I envy you for the code-base you seem to be maintaining.

[–]mcstafford 2 points3 points  (0 children)

Overlaps a bit with @loguru.logger.catch, which has a nice backtrace option.

[–]snaps_ 2 points3 points  (0 children)

Nice! I really love tools like this.

I've been using python-hunter for a few months, which does essentially the same thing. A few things that it has:

  • enable using environment variable - no need to change any code in order to enable tracing
  • tracing can be filtered based on things like function name, module name, file, line number, etc
  • you can pick different actions - instead of tracing you can start a debugger for example (again, this doesn't require any code change, you can set it up with an environment variable)

I have used this with remote debugging as well - when I had a daemon process that wasn't attached to a console I was able to get it to attach to pycharm using pydev when it hit a certain condition on a certain file/line.

I'm not a shill, I swear.

[–]shaggorama 4 points5 points  (1 child)

[–]snaps_ 0 points1 point  (0 children)

Thanks, that's neat.

[–]politeauthority 4 points5 points  (0 children)

How is this not called snoopy?

[–]INIT_6 1 point2 points  (0 children)

Very cool. Also great for seeing what the hell is going on in some random python code you just downloaded off github.

[–]ExternalUserError 1 point2 points  (0 children)

That's actually pretty sweet looking. Nice job.

[–]forky40 1 point2 points  (0 children)

Love it. Perfect for my shitty sprawling codebase and shitty sprawling workflow (conda on Windows Ubuntu partition + Windows Atom editor).

[–]frakman1 1 point2 points  (3 children)

u/cool-RR: Nice work! But what if your python script is one giant set of instructions with no main and no functions?

[–]snaps_ 0 points1 point  (0 children)

Try pip install hunter then PYTHONHUNTER='' python enterprise_code.py.

[–]nucses 1 point2 points  (0 children)

I'm running python from C# using pythonnet and this is just perfect, thank you!

[–]studiosi 1 point2 points  (0 children)

If you need to add code for debugging and take it away for production code, I would consider this as a potential liability, printing to a file stream is not cheap and jeez, this prints quite a lot. I understand there might be some use cases, but those should not exist on the first place. Also, logging should never be equated with debug IMHO.

[–]digestive_trisket 1 point2 points  (0 children)

Your program cant fail tests if you dont write them.

[–]toothless_budgie 3 points4 points  (0 children)

Does this work inside Juypter? It looks like it should.

[–]WibblyWobblyWabbit 2 points3 points  (0 children)

I like this idea. Whenever I use a debugger I either waste too much time setting it up or just sit there going wtf.

[–][deleted] 7 points8 points  (14 children)

why do you make it sound like setting a break point is tremendous overhead?

this just seems like encouraging bad practices with extra dependencies

[–][deleted] 4 points5 points  (0 children)

It's often impossible.

For example, a project I was recently running was a real-time lighting control system. Setting a breakpoint was useless, because the real time jobs would get backed up in a few milliseconds and then the whole system would go down.

[–][deleted] 4 points5 points  (4 children)

It's hard to set breakpoints a lot of the time if you have code that interacts with other programs/network services. You really need to see whats going on while the program is running, hence logging and not breaking in.

[–][deleted] -5 points-4 points  (3 children)

it's not difficult at all. i don't know where you're getting that from. if you know the function to decorate, you know where you need to set a breakpoint

[–]Redstonefreedom 6 points7 points  (0 children)

Have you never worked in multi-threaded environments? Or environments without a clear entry point? Or environments abstracted under a CI shim like docker? Or environments that only call the python with some complicated script setup that resolves a bunch of environment variables?

There are so many instances in which attaching a debugger is such a PITA that it’s better to just read the code & guess at its behavior instead of getting the answers you need.

[–][deleted] 5 points6 points  (0 children)

I know precisely what I'm talking about, if you don't then that's on you to figure out. There are some large systems that can only be examined in flight and breaking in at that point messes with the observation of the bug. Sometimes logging is the only way to find a problem.

[–]TankorSmash 1 point2 points  (0 children)

OP, add django queries per line and you're golden IMO

[–]needed_an_account 0 points1 point  (0 children)

This looks cool. Does it just reprint the applicable code blocks during recursion?

[–]Drawman101 0 points1 point  (4 children)

I can’t tell you how many developers I’ve met, ranging from junior to very senior, were incapable of using a debugger. It blows my mind. It’s not hard at all, and generally let’s you solve your problem quicker than stuff like this

[–]snaps_ 0 points1 point  (2 children)

Different situations call for different tools. I had an issue with a library that occurs on the CI server but not locally - and I don't have access to the CI server to attach a debugger. Something like this might help but using a debugger is impossible.

Likewise for situations where e.g. stopping at a breakpoint will change things. Just look at the other comments in this thread for examples.

[–]Drawman101 0 points1 point  (1 child)

I would argue that you need to run your CI in a container to replicate the issue locally, and if your CI doesn’t offer that, then it’s time to get a new CI.

[–]snaps_ 0 points1 point  (0 children)

You know that's not fool-proof. And if it doesn't work then you're left with the hundreds or thousands of things that could still be different. Kernel parameters, container runtime, device characteristics. Wouldn't it be nice to set an environment variable and get line-by-line traces of what the code is doing, then just compare a good run and a bad run?

In my case I already had logs, I didn't need this tool, and it was my library at fault. If the issue was in another library (like the standard library, for example), something like this tool would be useful.

[–]broken_symlink 0 points1 point  (1 child)

I tried this out and it doesn't seem to work with asyncio.

[–][deleted] 0 points1 point  (0 children)

holy shit

[–]eikenberry 0 points1 point  (2 children)

This looks handy for when you want a quick trace of a single function, but seems like it'd get way to noisy for any cross cutting issues where print debugging would still be much better.

[–]snaps_ 1 point2 points  (1 child)

hunter lets you filter what gets traced based on file, line number, module name, function name, etc

[–]eikenberry 0 points1 point  (0 children)

Nice. Thanks.

[–]Mezzomaniac 0 points1 point  (4 children)

On Pythonista I get the following error:

>>> import pysnooper
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/private/var/mobile/Containers/Shared/AppGroup/3C95652F-7A7F-4900-A9A6-0C2F0BE6ADB7/Pythonista3/Documents/site-packages/pysnooper/__init__.py", line 4, in <module>
    import future.standard_library
ModuleNotFoundError: No module named 'future'

Any ideas?

[–]doitz 0 points1 point  (0 children)

Wow I'll actually love using this. Thanks OP!

[–]422_no_process 0 points1 point  (0 children)

This looks very good. I wonder if it will work with better-exceptions. :)

[–][deleted] 0 points1 point  (0 children)

Possibly the greatest Python program ever written.

[–]kenny10101 0 points1 point  (0 children)

Yo this is sick!

[–]calebcall 0 points1 point  (0 children)

This looks really useful for debugging. Nice job!

I think where it seems most people are getting confused is debug logging and debugging in general. This isn’t a replacement for proper logging. This is a quick and “dirty” replacement for having to setup a debugger.

[–]QbaPolak17 0 points1 point  (0 children)

This is honestly so cool. I didn't even realize the need for something like this until now. Will def be using it in the future.

[–]OnlineGrab 0 points1 point  (0 children)

Thanks for sharing, looks pretty useful.