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

443 Upvotes

66 comments sorted by

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.

6

u/kyo22 Oct 04 '21

I need to get into unit tests. What are your thoughts about unittest vs pytest?

11

u/fiddle_n Oct 04 '21

Both will get the job done fine. Neither is a huge investment to learn; if you start off with one you can easily learn the other. With that said, having used both (unittest at work; pytest at home), I do prefer pytest.

  • pytest is more Pythonic - you can write tests in top-level functions and use the built-in assert keyword. unittest will force you to write a class for every test module, even if you only have a handful of test cases; unittest also uses its own assert methods rather than the built-in assert.

  • pytest has more features - for example, it contains a way to parameterise tests, so that you can run the same test with multiple inputs if required.

  • pytest has backwards-compatibility with unittest - if you write tests in the unittest style, you can use the pytest runner to run them. So you can move from unittest to pytest without losing any of the tests you've previously written.

2

u/synthphreak Oct 04 '21

Fantastic response and agree on all counts. Hat's off.

4

u/AnonymousLad666 Backend Developer Oct 04 '21

I tend to use unittest mainly because is a std module, but in the end is all personal preference.

4

u/choff5507 Oct 04 '21

Any good sources for learning unit testing ?

12

u/IlliterateJedi Oct 04 '21

https://realpython.com/pytest-python-testing/

Any time you think "I am going to check this function in the console" make it a test instead. It makes life a million times easier down the road.

1

u/Liberal__af Oct 04 '21

What are the other drawbacks of unit test module besides writing a chunk of the same code again and again? Just wondering!

3

u/IlliterateJedi Oct 04 '21

I personally don't have experience with the built in unittest module. I have always used pytest because of how straight forward it is to use and how cleanly it works in pycharm.

2

u/AnonymousLad666 Backend Developer Oct 04 '21

There are no drawbacks to unit testing, you don't write the same code again, if you do then you're doing it wrong.

  • unit testing tells you this class, module, piece of code is good

  • make one change downstream or upstream, unit test fails, caught it before sending to prod

  • in a huge project, manual testing is just not feasible, nor sane

  • unit tests give you a % coverage, you'd want as high as possible

  • unit tests also work in automated CI pipelines where multiple SREs are working on a code base

Think about it this way the code you're testing against is a black box, and the unit tests are testing with different arguments making sure the return value is what is supposed to be. More often than not in a business, these are two different engineers.

1

u/PM_ME_CUTE_FRIENDS Oct 04 '21

To me, pytest just feels more pythonic in the way it tells you how unit tests should be. Its features are also simple to understand.

As a side note, repetitive code in unit test is not bad per se, and is actually quite common. You want to be able to see the sequence of steps in whatever you are testing.

1

u/fiddle_n Oct 04 '21

I'm not sure I follow your question. Are you talking about what the drawbacks are of manual testing (i.e. writing code in the shell repeatedly) vs automated testing (using unittest/pytest to write tests)?

1

u/synthphreak Oct 04 '21

Unit tests are unrelated to code repetition.

Unit tests are specialized objects that test whether a function is bug-free and working as expected; there are major benefits to unit testing, and the only drawback is the extra time needed to write and maintain your tests. By contrast, code repetition is just bad practice.

If you are writing code where a single chunk is repeated again and again, consider encapsulating that chunk as a function. That way you can just write the logic of that chunk in one place (i.e., inside the function), then simply call that function again and again as needed. That way, if the logic ever needs to change, you only need to change it in one place. You can also then write a unit test for that function to essentially keep tabs on it and ensure that it keeps working even as other changes are made elsewhere in the codebase during the development lifecycle.

In this way, unit tests help you easily test your entire code ecosystem to confirm that all the components continue working nicely together even as you make tweaks.

1

u/AnonymousLad666 Backend Developer Oct 04 '21

https://docs.python.org/3/library/unittest.html

I would recommend codewars too, since all the challenges will be unittested. Do a challenge, and setup the unittests on your local machine.

3

u/Sajuukthanatoskhar Oct 04 '21

Last year, my boss (german, 60, 30yr XP in perl, C, python, embedded systems) told me off for using the debugger so much.

He mentioned that printf's or UART transmissions were better (in tandem with a logic analyser) ajd that i shouldnt be so lazy. From day 1 of coding, i have used the debugger to step through unfamiliar code.

I asked some of my colleagues at my new job and they use the debugger quite a bit.

I didnt really care for his comments but apparently i do, because i have been thinking about it since then. Is using the debugger that bad? I use it to explore the context, get to a step and see what I can do to it by injecting lines of code and seeing the result since documentation was hard to come by.

3

u/AnonymousLad666 Backend Developer Oct 04 '21

Software Engineer here 10 years of experience. Using a debugger isn't lazy, nor a bad practice. If something doesn't work or you want to see how it works internally use a debugger. That's why they exist. I don't see how using print statements make you not lazy lol. I don't work in embedded nor German so not sure where he's coming from with that thinking, but is a bit weird to discourage debugger use.

1

u/fiddle_n Oct 04 '21

That's probably one of the dumbest things I've read. It reeks a bit of "real programmers use X", where X is a low level programming language or editor or whatever. Debuggers are insanely useful. It's not lazy to use a debugger; it's a waste of time to write print statements directly in the code everywhere.

0

u/[deleted] Oct 03 '21

[deleted]

8

u/doulos05 Oct 03 '21

Most people are, it at least they feel like it.

0

u/[deleted] Oct 04 '21

[deleted]

1

u/thoraldo Oct 04 '21

Did they remove pycharm community?

0

u/[deleted] Oct 04 '21

[deleted]

2

u/[deleted] Oct 04 '21

Because it doesn't directly generate revenue

1

u/AnonymousLad666 Backend Developer Oct 04 '21

I tend to use pycharm and vscode. Overall I like jetbrains tooling since you get the same feeling across language IDEs. I get the free license for maintaining open source 😁. Been a fan for years since I switched from eclipse to intellij.

1

u/fiddle_n Oct 04 '21

Honestly, once you learn how to use one debugger, it's very easy to use the others.

The basics of debugging are universal - set a breakpoint, run the code until the breakpoint, step through the code line by line and inspect variables, use the REPL to do more advanced introspection if required, know the difference between stepping into a function call vs stepping over it, go up and down stack frames if required, and then run the code until the next breakpoint.

Once you know the above, using other debuggers is just a case of figuring out how to do all those things in the new debugger, and maybe taking advantage of more advanced features (e.g. conditional breakpoints in PyCharm).

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

u/Chaluliss Oct 06 '21

I appreciate the response and perspective, thank you!

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

u/synthphreak Oct 04 '21

shit , shave and shower

That is indeed the proper order.

2

u/[deleted] 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

u/[deleted] Oct 04 '21

[deleted]

1

u/pythonista37 Oct 04 '21

Facts, it's super sweet!!

4

u/[deleted] Oct 03 '21

VS code standard Python extension is good

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

u/Jamarac Oct 04 '21

Built in has been all I've needed in my 1+ year of coding.

2

u/[deleted] Oct 03 '21

Thanks for this post.

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

u/flufylobster1 Oct 04 '21

Import pdb

1

u/synthphreak Oct 04 '21

pdb ipdb

:)

Also, I'm pretty sure you don't need to manually import pdb. Unless you have set a custom debugger e.g., via the PYTHONBREAKPOINT environment variable, just inserting breakpoint() into your code will call pdb.set_trace.

1

u/[deleted] 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

u/[deleted] Oct 04 '21

SLIME does that kind of stuff (similar to how you describe PyCharm, except, you don't need to click anything). pdb could'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

u/ErrorFreeCode Oct 11 '21

I'll let me name speak for itself.

-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

u/[deleted] 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?