r/learnpython • u/kyo22 • Oct 03 '21
Learning your IDE Debugger will change your life
Don't make that same mistake that I did. I've been coding for more than 6 months always debugging by using print functions, spending several hours trying to run the code a thousand times hoping to get it right eventually.
When I tried to use the debugger on my own it seemed too complicated, stuff for advanced users, and never really gave it a shot. But in the past week, I decided to learn how it works, and holy fucking shit I could not believe it.
You can do so much stuff with it it is incredible. Not only you're able to keep track of all variables as the code runs line by line, you can tell it to print the variables if would like, you can set conditional breakpoints, you can evaluate new things mid debugging session like a scratchpad calculator for code. In Pycharm it allows you to see dataframes with a beautiful layout with color formatting.
I wish I knew this when I was beginning. My life would be so much easier. My productivity increased so much I feel dumb for not stopping to learn the debugging tools earlier.
Please, if you don't know how to use the debugging tool, take 30min of your day to learn it. It will change your life.
I used the first half of this video to learn the Pycharm debugger: https://www.youtube.com/watch?v=k6j1NkVAsuU
34
u/mopslik Oct 03 '21
Agreed. The debugger has helped reduce the number of questions my students ask, because they can see for themselves how things are changing and how values are interacting with their functions. We use Thonny, as I find it easier for beginner programmers to set up and use, and I have waxed poetically about its debugger in the past.
2
u/Chaluliss Oct 04 '21
Since you have students and I am currently a student, a relatively new programming student at that, would you suggest any resources on debugging to get started?
If it means anything the IDE my java class has me using is Eclipse, would you suggest IDE specific debugging resources or is there a more generalized approach to learning you would suggest as a teacher?
Sorry to put you on the spot, and if anyone else feels inclined to offer advice to me, please do! I just want to equip myself with the best tools to become a better problems solver early on.
6
u/mopslik Oct 04 '21
I teach high school Comp Sci, so we just get into the basics in the third year (input/output, conditionals, loops, functions, arrays) and expand on some of that in the fourth year (OOP, algorithm design/efficiency, data structures). Typically, after we learn enough commands to start making programs that are more than a few lines long (that is, sometime during loops and conditionals), we look at how we can use the debugger to avoid littering our code with print statements and test cases.
I don't have any real suggestions about using the debugger, other than to just use it. The sooner you get a feel for toggling breakpoints, or knowing when to step in vs. step over, the sooner you'll reach for it as a tool when you're working on a complex project. And for the most part, the students find it fairly easy to navigate, once we've spent a class or two working through examples together and practicing on some simple pieces of code.
1
2
u/choff5507 Oct 04 '21
Have you tried IntelliJ for your IDE?
1
u/Chaluliss Oct 06 '21
No, but am considering giving it a shot... Though considering I am already partially familiar woth eclipse I may just stick with it.
2
u/choff5507 Oct 07 '21
I’ve used both. I honestly think you’ll really come to love IntelliJ if you give it a chance. And I’m not a jetbrains fanboy or anything. I don’t like pycharm and use vs code instead for python.
1
u/Kenkron Oct 04 '21
IIRC, in eclipse, you can click the the left of a line number in a file to make a breakpoint (it will appear as a red dot). Then, up by the "run" button, there should be another button to debug. (Just to the right. Looks the same as the run button, but with a little bug on it). It might ask you about switching to debug perspective at some point. Press okay to that, and it will add a debug window.
Now, whenever your program gets to the line of the code with the breakpoint, it will pause and hilight that line. The debug window should have some tabs for stack frames and local variables. The local variables will show you the value of all the variables in that method call. The stack frame will show the method that's being called, as well as the method that's calling that, and the method that's calling that, all the way up to the main method. Selecting any of those will cause eclipse to switch to showing that method, and the local variables inside of it.
This is all happening while the code is paused, so if you want to continue (until another breakpoint is hit) you can (it's a little play button). If you want to continue only up to the next line, you can (called "step over").
It's pretty helpful.
1
u/Chaluliss Oct 06 '21
Thanks for the detailed response, I will be using this in my upcoming work/assignments to improve my workflow!
4
u/Covered_in_bees_ Oct 04 '21
Pycharm debugger and terminal integration for interactive coding/debugging is simply outstanding and the primary reason why I never have transitioned away to VSCode. The latter is great in many ways but debugging and integration with an amazing interactive python/ipython terminal with excellent variable viewer support makes Pycharm so much better than VSC. Iterating on code and/or fixing bugs is so stupidly easy in Pycharm that I will often times just stub out new classes/functions I am writing, break point into them with the params I plan to use and just interactively develop the entire function/class while catching tons of stupid mistakes you'd normally have made if you were coding "blind" by coding, running, printing/logging and then repeating.
1
u/BKKBangers Oct 04 '21
As a beginner I tend to go into auto intimidated mode when I see words like Advanced in a title; maybe just me, probably is, but perhaps the yt vids title should be re-written which might make the debugger seem less scary (just a theory)
2
u/kyo22 Oct 04 '21
It's pretty simple actually. Give it a shot.
3
u/BKKBangers Oct 04 '21
I just added it to my bookmarks; getting to it first thing after I had my shit , shave and shower.
1
2
Oct 03 '21
Can anyone recommend a good VS extension for Python? Or is there a good builtin?
I've usually used Python's logging module for this kind of thing and it's unideal for sure.
5
u/pythonista37 Oct 03 '21
I just use breakpoint(), works regardless of IDE
2
u/PM_ME_CUTE_FRIENDS Oct 04 '21
Huh TIL. Python 3.7+. I still
import pdb; pdb.set_trace()if I'm not on an IDE, old habits.0
4
3
u/laundmo Oct 04 '21
if you just search python in the extension market theres one that's just called "Python" at the very top. that is the one you need, tho i wonder how you have been coding python without it.
1
2
2
u/p000l Oct 04 '21
If you are learning (or teaching) Python, check out Thonny's (Nicer) debugger, Run > Debug current script (Nicer).
It makes a nice visual representation as you go through the breakpoints, replacing the values for your variables and stuff. It's quite good fun too.
2
u/PM_ME_CUTE_FRIENDS Oct 04 '21
I'd like to point out that there is nothing wrong with print statements. It is the most lightweight, portable debugger that you can ever have. There are some scenarios that you need to debug without an IDE and those could be times where you'll be glad you know enough about using the ol' handy print debugger. That said, learning your debugger is a game changer especially for very large projects.
1
1
u/flufylobster1 Oct 04 '21
Import pdb
1
u/synthphreak Oct 04 '21
pdbipdb:)
Also, I'm pretty sure you don't need to manually import
pdb. Unless you have set a custom debugger e.g., via thePYTHONBREAKPOINTenvironment variable, just insertingbreakpoint()into your code will callpdb.set_trace.
1
Oct 04 '21
Idk. I never really learned to use GUD. I just use external debuggers / whatever comes with the language. Why does it have to be the one that comes with your IDE?
Also, when it comes to Python, the built-in debugger (i.e. pdb is kind of... not so great because it cannot deal with far too many things. Like, for example, if your code decides to do something to stdin, gdb will not undo it. Similarly, if your code uses threads or processes, gdb is helpless there. Still better than nothing of course.
1
u/fiddle_n Oct 04 '21
I think you can still get benefit from something like Emacs GUD. Being able to see what's happening to the variables as you step through the code automatically, rather than having to query each variable separately, is very useful. It jumps out at you what's going wrong much quicker.
However, the benefit of using a full GUI debugger, like the one in PyCharm, is where it really shines IMO. For example, not only do you get the benefit of seeing all the variables at every moment, but nested data structures have a small dropdown arrow to let you individual look at each element (and if those elements are nested themselves then you get another dropdown arrow and so on). It's a godsend when querying nested data structures; just clicking the arrows I care about rather than doing
list[0][6][3]or whatever.0
Oct 04 '21
SLIME does that kind of stuff (similar to how you describe PyCharm, except, you don't need to click anything).
pdbcould've done that too, if more efforts were invested into it.I'm not against comfort. Just saying that there isn't a reason to tie that comfort to the debugger built into your editor. Other debuggers could be just as good / better.
2
u/fiddle_n Oct 04 '21
To be clear, PyCharm will also show an inline view of what the nested structure looks like, the drop-down just switches it to a vertical view which is far easier to read.
I think the reality of it is that pdb is a "gets the job done" type of debugger and for people who use the common editors for Python, either PyCharm or VS Code, the included debugger works fine for them. I certainly have no reason to switch away from using the PyCharm one.
1
u/btgrant76 Oct 04 '21
Nina Zakharenko's PyCon talk, "Goodbye Print Hello Debugger" is excellent. While in-IDE debuggers are discussed, a big chunk of the talk is focused on debugger tools that can be run outside of an IDE.
I'm well-acquainted with PyCharm's debugger at this point, but it was super cool to learn about these built-in debugging options.
1
u/synthphreak Oct 04 '21
Is there something magical about "IDE debuggers"? I just use ipdb.set_trace, and I agree it is a totally game-changer. But I just use it in an interactive shell (where I do all my development), so never inside an IDE. In fact I don't really use IDEs at all.
So is there anything different about how I do it compared to doing all my debugging via an "IDE debugger"?
0
-33
u/antiproton Oct 03 '21
I can't fathom this idea that the debugger is "too complex". You set a break point where you want the code to stop, then you press "debug"...the code runs until it hits the point you marked, then it waits.
While it's waiting, you can look in the little debug panel to see the values of all your variables.
From here, you can let it continue to run, or you can run it one line at a time.
Thus isn't much different than washing your dishes by hand because figuring out how to start the dishwasher is "too complex".
32
Oct 03 '21
All things are hard when starting out. The UI for a debugger might look intimidating. Someone might not know how to interpret what they're looking at. They probably don't know what a breakpoint is yet. For someone just starting out, it's not hard to think a debugger is gonna be off-putting at first.
6
u/nekokattt Oct 03 '21
Now GDB... that is intimidating!
1
u/Kenkron Oct 04 '21
Lol, that's the first debugger I learned, and it was crazy. It was still a big upgrade from print statements though.
3
u/vardonir Oct 03 '21
One man's too simple is another man's too complex.
Going by your dishwasher example: I didn't know that you're supposed to get the solid food gunk off the plates before putting it in, I didn't know that only certain plates and utensils are safe to put inside the dishwasher, I didn't know that they run for hours and hours, I didn't know they had special dishwasher detergent, etc.
And this is because I didn't know what dishwashers - the electrical appliances - were until I was like 20 because, in my house, we had dishwashers - people with hands standing by the sink.
-3
u/antiproton Oct 03 '21
And did you look at the appliance, throw your hands up and say "well, I'll never figure this out?"
Because that's what we're talking about here. Not trying and failing, but just refusing the attempt.
If you want to be a developer, you kinda need to be a problem solver. If you can't even solve the problem of "how do my tools work?", how are you going to make a career out of this?
3
u/doulos05 Oct 03 '21
Sure, but a lot of people learning python just wanna write their little pet project or run quick pandas operations on datasets. They don't want to be developers who solve lots of problems, they want to solve their problem. And learning the debugger isn't the problem they wanted to solve.
That doesn't mean they won't benefit from solving it, they will. But "you're a software dev, learn to solve problems" isn't an effective line to take with them.
1
u/Thifty Oct 04 '21
I know several people who prefer washing dishes by hand. It’s not that feasible to imagine people being comfortable with what they know…
1
u/vardonir Oct 04 '21
Pretty much, yeah. The appliance is intended for large families. I have a two-person household, I don't have that space in my kitchen, I don't want to buy new plates.
What if I'm not a software dev? What if I'm just someone trying to learn python so that I reorganize a bajillion files, or automate another program? I don't need a debugger for that.
I don't need it, why should I learn it?
112
u/AnonymousLad666 Backend Developer Oct 03 '21
Debugging and unit testing is something beginners should learn early on, yet most courses and books barely talk about it.