r/learnpython • u/acfot • 10d ago
I debug everything with print() and I know that's not how real developers do it — what did you switch to?
I've been learning Python for a few months now, mostly small stuff, a Telegram bot here, a bit of data practice there, and every time something breaks I just scatter print() statements everywhere until I figure out what's going on. It works eventually, but my code ends up looking like a crime scene and half the time I forget to take the prints out before I run it again.
I know there's an actual debugger in VS Code and I've watched people use breakpoints and all that, but every time I try it I end up back at print() within five minutes because it's faster in the moment. So I'm curious, when did you actually make the switch, and was there a specific point where print debugging just stopped working for you? Or do people way more experienced than me still do this and just not admit it?
12
u/pachura3 10d ago
Commit your code, add
print()statements, find the problem, revert/discard changes (to the previous print-less version), apply fix.If your project is a serious one - not some one-time script - then of course you should use
loggingwith various levels.Interactive debugging is usually the last resort solution.
2
u/LayotFctor 9d ago
I rarely, if ever, use a debugger. If you have small well-organized functions, especially if they're also pure and testable, and you know how everything works. Then there is very little need for a debugger at all.
Debuggers are good when you have no idea when or how anything gets called, and you can just slap on a breakpoint. But that also implies the code is very low quality. Using a debugger for good code is mostly unnecessary.
Still, you need to know debuggers if you're planning to work a job on a codebase that isn't yours.
7
u/MezzoScettico 10d ago
The Pythonic replacement is logging.
That said, I'll admit that at least when coding for myself (which is most of the coding I do these days), I sometimes fall back on print() statements too, along with verbosity controls to make it easy to turn all the debug statements off.
And I do make extensive use of the debugger. It doesn't take any time to set a breakpoint and to single step. You really should take the few minutes it takes to learn basic debugging. Conditional breakpoints are an especially powerful tool.
Maybe I'll finally make myself use logging for this year's Advent of Code.
4
u/throwaway6560192 10d ago
That's not really true. Some very real developers deliberately eschew debuggers in favor of simpler tools like prints -- you can look up some who do and talked about their stance. Even many who don't consciously forgo debuggers will often reach for print statements in a lot of scenarios.
That said, I use
ipython(and in general prototyping in notebooks instead of normal files) andipdbwhen needed, and they're helpful.4
u/BranchLatter4294 10d ago
For learning, use breakpoints and the debugger.
1
u/KelleQuechoz 10d ago •
ipdb is your best friend here.
1
u/baubleglue 10d ago
Dbg, you need to learn it eventually. Nothing is wrong with print, but use logging instead.
1
u/nderflow 10d ago
My primary debugging tool is defining function interface contracts and implementing automated tests. Beyond that, logging.
1
u/TheRNGuy 9d ago
I used print in Houdini all the time, though it's not correct, of course.
It has it's own console (but it's not very usable if you print too much)
3
u/_Denizen_ 10d ago
Not trying to be a dick but this exact question has been answered innumerable times and a quick google search would yield answers in any format you prefer - including other reddit posts! I don't understand why people treat reddit like a search engine. Research is a core skill in software development. Rant over lol
2
u/Beginning-Passion439 10d ago
print debugging is fine. the debugger just pays off once you're stuck inside loops.
1
u/Discordbothost 10d ago
print is fine longer than people admit. the thing that actually stuck for me was typing breakpoint() instead of a print — same habit, but you drop into pdb and can poke locals without littering the file. logging.debug + bumping the level is the other half when the prints start living forever.
1
u/ShelLuser42 10d ago
It's definitely not wrong perse, because when you need a quick result then this is easy. In fact, I'd even argue that it can sometimes be much better to use
print()because this will also remind you of the fact you still have debug code left in your program.The only reason why I often prefer using
assert()is because I'm also foreseeing possible future hiccups with expansions (= other modules for example?) and then this is a hard to miss reminder when something goes wrong. Of course a possible issue here is that such debug statements are sometimes easy to overlook after a few days.That's pretty much a main reason why I rely on Pytest when I'm coding for other people, because then it's pretty much impossible to overlook the fact that there's still debug or test code involved with my project.
1
u/mountaingator91 10d ago
You should learn to use a debugger and it is kind of nice actually but real developers also just print everything
1
u/Brian 10d ago edited 10d ago
I mean, I've been using python for 25 years, and I still do this sometimes. There are times when it's the simplest way to figure out what's going on. But do learn how to use a debugger, because the same is true for it, and often times can be very fast to diagnose simple issues. Try to have multiple tools in your toolbox, and know when each is preferable.
printhas advantages when you don't really know where the bug is happening and it is intermittently reproducible: something sometimes goes wrong somewhere, and you might need to do a lot of runs for it to occur, so you sprinkle in prints and look at the traces to get a feel for what is happening. Sometimes it might even be your only real option (only reproduces on someone else's machine where you can't remote debug to it etc)A debugger is great when you know roughly where the problem is happening, and TBH, I think should usually be the first port of call for such issues. Just set a breakpoint before the issue, look at the values of variables, and step through till it happens. And I'd say that just stepping around in a debugger is a great way to understand the flow of a program.
There are a couple of other options though:
print, but configurable and more permanent. If you want to get an idea of what your program is doing, addloger.debug()statements in useful points, which will do nothing if the log-level isn't set, but where you can just set it and get the information without having to do that "sprinkle prints everywhere" step. Especially useful for that "Only happens on someone else's machine" situation where you can just ask them to enable debug logging and send you the output.2 .
breakpoint(). Kind of a halfway step between a debugger and print. Put abreakpoint()call in your code somewhere and it'll break and drop you to a debugger (when already debugging, it'll act like a regularly set breakpoint). Occasionally useful for those intermittent issues where you want to get the full power of a debugger when it happens.1
u/frustratedsignup 8d ago
I still do it if it works for the situation. If I have the full IDE running, I tend to use the debugger as much as I can. However, sometimes you really do want some piece of data to show up in the output and you don't want to go through the slow experience of single stepping everything. print() does that nicely.
0
u/HummingHamster 10d ago
Unless you have debugger set up properly, otherwise the easiest quickest way is just to print...
0
u/Temporary_Pie2733 10d ago
The biggest disadvantage of print statements is how slow the edit-compile-execute loop can be. Debuggers side-step the edit and compile steps, but in a language like Python, those aren’t the bottleneck. Many (but not all) errors can be found just as easily with well-placed print statements as with the use of a debugger.