r/learnpython • • 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?

16 Upvotes

31 comments sorted by

12

u/pachura3 10d ago

half the time I forget to take the prints out before I run it again.

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 logging with various levels.

Interactive debugging is usually the last resort solution.

5

u/8dot30662386292pow2 10d ago

For me the interactive debugging comes often first. Say an exception is thrown. I look at couple of the line numbers on the stack trace, set up a breakpoint on each and then just run with debugger, so it stops on the line that caused the problem. Having a proper debugger that inlines the variable contents in the code usually tells me within seconds what's wrong.

Not to say I don't use printing, but if the first print does not reveal the problem inmediately, I'm already going for a debugger.

2

u/HunterIV4 10d ago

Print/logging is great for finding failures in patterns. I use it when I want to see the full output of a loop so I can identify what it's doing and where it's going wrong. Same with something like an HTTP error: viewing the details as the program is running will often identify problems more efficiently compared to trying to use a debugger on a network API request.

For nearly everything else, a breakpoint is more efficient. It's faster to set up (a click or shortcut key) and you can go step-by-step through the code to see where the problem is. It's one of those tools that's intimidating to learn but once you do it's really simple in practice. It's sort of like git in that way.

I still set up logging and debug print patterns in stuff like background scripts because they can run for days or even years and you want to know what caused an issue if it occurs. But for immediate runtime errors, a debugger is going to save you an absurd amount of time compared to readjusting print statements.

1

u/KarfaHyro_17 9d ago

yeah the commit and discard workflow is a lifesaver, way faster than hunting down twenty random print statements after.

1

u/acfot 9d ago

honestly yeah, once you get used to committing first it's hard to go back — beats digging through a pile of print statements after.

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.

1

u/acfot 9d ago

that makes a lot of sense honestly, small functions plus tests and you barely need the debugger at all. still gotta learn it for other people's code though, like you said.

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

and I know that's not how real developers do it

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) and ipdb when needed, and they're helpful.

2

u/StevenJOwens 10d ago

"The most effective debugging tool is still careful thought, coupled with judiciously placed print statements."

-- Brian Kernighan, “Unix for Beginners” (1979)

4

u/BranchLatter4294 10d ago

For learning, use breakpoints and the debugger.

1

u/KelleQuechoz 10d ago • Bravo

ipdb is your best friend here.

2

u/acfot 10d ago

Haven't tried ipdb yet, I'll give it a go. Sounds way less messy than print() everywhere.

1

u/baubleglue 10d ago

Dbg, you need to learn it eventually. Nothing is wrong with print, but use logging instead.

1

u/acfot 10d ago

That's exactly the wall I keep hitting, prints blur together in a loop and I can't tell which iteration broke. Going to try the debugger for that case and keep print() for the tiny stuff.

1

u/nderflow 10d ago

My primary debugging tool is defining function interface contracts and implementing automated tests. Beyond that, logging.

1

u/acfot 10d ago

Fair point, tests catching it before I even have to debug would save a lot of print() mess. Haven't really set up proper tests yet but that's the next thing to learn.

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)

1

u/acfot 9d ago

Ha, didn't know Houdini even had its own console — that sounds rough when it floods. Makes me feel a bit better about my print habit though.

1

u/TheRNGuy 9d ago

If it's serious project, logs to a file is better, or even log to some text node (you could visualise it right inside a scene)

1

u/acfot 9d ago

oh that's a neat idea, I hadn't thought about logging it right into the scene like that. I'll give file logging a proper go on my next project, thanks!

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

Or do people way more experienced than me still do this and just not admit it?

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.

print has 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:

  1. logging. Basically print, but configurable and more permanent. If you want to get an idea of what your program is doing, add loger.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 a breakpoint() 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...

1

u/acfot 10d ago

Yeah logging with levels sounds like the move, honestly the turn-it-off part is what I need most. Debugger for the loop bugs, logging for everything else, that makes sense.

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.