r/pythontips • u/[deleted] • Mar 24 '21
Python3_Specific How to debug in Python?
Please share some tips about the title.
9
u/Abiogenejesus Mar 24 '21
I use PyCharm's built-in debug functionality, which is great. Most Python-capable IDE's have debugging tools for Python or at least plug-ins to facilitate debugging.
12
6
u/miamiredo Mar 25 '21
Really surprised no one mentioned:
import pdb
pdb.set_trace()
I put that anywhere I want to inspect what is going on and run the script! Then you can see what variables are at that point and also go through the code line by line!
2
1
u/zaxswyre Mar 26 '21
New in version 3.7: The built-in
breakpoint(), when called with defaults, can be used instead of
import pdb; pdb.set_trace().
7
u/Karam2468 Mar 24 '21
What does debugging do anyways? Newbie here. Why and when shoud we use it?
10
u/SHKEVE Mar 24 '21
It’s the process of “stepping through” code either in chunks or line by line and seeing what’s going on. Say you have a function that’s returning an unexpected value, you can go through each line of your function as it executes and watch what happens to your variables to catch where the error is.
3
u/VeritosCogitos Mar 24 '21 edited Mar 24 '21
Have you used bugged code? Where you expect one result and get another?
Is that what you want to produce?
The more frequently you test your code to make sure it works is better than finishing a project and finding nothing works and need to narrow it down.
Any typo can cause a bug, and sometimes the bug checkers won’t even catch that. My least favorite debugging error is syntax error with no clue where the error takes place lol.
1
Mar 25 '21
"debugging" de-bugging, bugs are errors in our code (if there are errors your code does not run, it just stops abruptly due to that error). de - means to remove and bugging - refers to bugs, so it literally means to remove bugs, removing errors in our code is called debugging
5
u/mishlawi420 Mar 24 '21
It might depend on your problem but i usually use print with random characters to check the section that it's giving me errors
2
Mar 24 '21
I run my code after I write every single part. A part can be a single line, or maybe it's a few lines I copy in. The main goal is that if I do have a bug, I know for sure that it is in the last section of code that I wrote because I'd been "testing" as I went along. This way, I usually am only ever chasing down a single bug at a time.
When you begin coding at a higher level, there are more formal ways to test if your software works in robust and automated ways, but they're not relevant to you yet. Be aware that this exists.
The actual process of debugging is pretty simple. If your code throws an error, it will tell you what the error is and where in the code the error occurred.
Read the error
If you do not understand some of the words in the error, look them up
Read the part of your code that made the error and reflect on why that may be
Look around for typos and basic syntax errors just in case
Most novice programmers don't do all those steps. Do all those steps. The next step is knowing how to find answers when reading the errors wasn't enough.
If the error is from some specific function or class from an external library, look up that function or class in the library's official documentation.
If the error is some Python specific thing, look it up in the official Python documentation.
If you're still stuck, phone a friend or ask online.
2
u/SpiderJerusalem42 Mar 25 '21 edited Mar 25 '21
Say you write some code, and you have the ability to debug in PyCharm. What you would do if you wanted to debug your program would be to set a break point by left clicking to the right of the line number you would like to set a break point. A break point will halt execution of the program before the execution of the line where the break point is set. From here, when you go to run your code, via the run menu (alt+'u'), Debug (Shift + F9) you might need to pick the program you want to debug with Alt+Shift+F9.
From here, you want to step through your code with the step commands and monitor the debugger window. More on the debugger window later. There are usually hot keys for moving through code execution. I think in the jetbrains suite, F8 steps over, F9 resumes, F7 steps into. To give you an idea what that means, steps over means that whole function call gets handled, and so long as it doesn't hit a breakpoint somewhere down the call stack, will move execution from the current line to the next one. Stepping into will attempt to go into the function implementation of any function calls you have made. If you make several calls on a single line, you might have to go into and out over several function calls before you get to the one you want to examine the functioning of.
The debugger window shows the value of various variables that have been defined in the running of your program. You can also set watches. You can use python expressions to define these watches, so you can take a part of a command that can be interpreted independently as an expression that has a value, and try to see why another expression didn't work out, while you were executing code. You can look at variables to make sure they are consistent with the logic in your head, as the code executes.
You can also right click on breakpoints and set a break condition. This is helpful if you hit a big loop, and don't want to step through to the condition you think is breaking.
I like to think of debugging as watching your code fail in a play by play, slow motion review.
1
u/Additional_Poem_6934 May 17 '25
Hi! I'm new to python (only starting out and on week one) but something really cool I noticed about error screens is that if you read thouse it tells you which line of code contains the error and it will tell you what type of error it is too. That helps a lot because it narrows it down. And granted, you have to repeat the process after fixing each bugged line but the error will always tell you where to look next. It's kinda like a find the bug guide. Hope this helps!
1
u/thecoolking Mar 24 '21
This is a great question. I use SAS a lot and it prints out all the errors, warnings and notes to a log. Python being more of a interpreted language rather than a compiled language does not have the feature. Using a jupyter notebook can help sometimes because every line can be split into a cell. Not so sure about thousands of lines though.
3
Mar 24 '21
Jupyter notebooks are crutches unless you're a data analyst. Then they're more like prosthetics than crutches.
1
1
1
Mar 24 '21
I would say use spyder along with the kite plugin
1
u/TheOneWhoSendsLetter Mar 25 '21
What version of Spyder do you have?
1
Mar 28 '21
Version 4.2
1
u/TheOneWhoSendsLetter Mar 29 '21
When you're in debug mode, can you see the debugging variables in the Variable Explorer?
1
Mar 29 '21
Yes you can. It's very much like MATLAB's IDE
1
u/TheOneWhoSendsLetter Mar 29 '21
How?! I've been trying desperately but haven't been able to see them
1
1
u/mlnet Mar 25 '21 edited Mar 25 '21
I've been programming for many years now professionally. Before I thought my greatest weakness was not writing lots of test coverage (I'm still terrible).
Like others commenting, print(thing) was my answer to everything. Or if I was getting fancy, log.info(thing).
But a year ago I was working on a data pipeline that finally broke me over its knee. Having always been frustrated what a slow learner I am, now I was really spinning my wheels in the mud.
So after years of coding I took a few days to learn everything I could about debugging. The takeaway for you is run a visual debugger. On Visual Studio Code you can use Control + Shift + D and then run (the play button), then Current Python File, or just hit F5.
Before I would try this like once, it looked weird and print statements still worked, so then I gave up. It's up to you to stick with it and use a visual debugger as THE only way you run your code as you write it and check it works. That means no running it with python myModule.py from the terminal. No copying into IPython or Jupyter Notebook and running cells.
What you are trying to see is the values and attributes of variables in your namespace all in one space. Visual debugging does this. You can use pydb to debug in code and on the command line. But don't.
VS Code Debugging Article. Read every word of this twice.
To wrap up this novel, becoming a good debugger by actually using visual debugging tools will catapult you up the ranks as a software developer. Why? Because it saves you time from your own inefficiencies. Debugging lets you catch the stupid thing that's not working in your code, fix it, check the fix fixed it, and move on to coding other things!
This last year my Impostor Syndrome has gone way down. I've billed myself as a senior developer for a while now. But I realized using debugging tools is one of the real marks of an effective developer. I am more confident with my ability to build apps / code out solutions to problems... because I am better and faster at writing code that does what it's supposed to. You ever hit a block where whatever it is your code is just not working? And you're stuck there for a day, or much longer? Not being good at debugging is why that happened.
You get good at debugging by debugging. Use Visual Studio Code's tools. Or any IDE. Learn how to start a session (F5). What in the world does stepping over / out, continue, the call stack mean? Try the visual debugger on your code and find out. Search online, "how to use callstack vs code debugger", etc.
It is too abstract a concept to learn from reading articles or this tome I'm writing you now. You have to do it yourself on your own code to really grasp what the hell debugging actually is.
Debugging is one of those dry seeming programming topics that you can get to later after you've sold your first startup for 5 billion dollars. But take it from a grizzled vet: debugging is the discipline you wish you forced upon your self Day 1 you started coding.
Quick example:
# myConfusingModule.py
import numpy as np
def unpredictableFunction(x: int) -> int:
""" multiply x by one of the values from an array """
choices = [None, 1, 2, 3]
choice = np.random.choice(choices, 1) # pick at random
product = x * choice
return product
if __name__ == '__main__':
x = 3
result = unpredictableFunction(x)
print(f"result is {result}.")This program will work... until it doesn't. (An integer can't be multiplied by None without throwing a TypeError Exception.)
You could run this a bunch of times and random chance might let it choose an integer each time to multiply with x. And outside the function, like in the if __name__ == '__main__': conditional, how would you debug what's going wrong inside unpredictableFunction? Or know it is the function that's the problem, and not whatever is being supplied for x in a real-world scenario?
"Then I'll put print statements inside unpredictableFunction until I find the problem." Okay. How it's written, that means you are printing choices, choice, product, then probably checking the return from the module invocation for good measure. But randomly choice can be an integer any number of times, and no problem will crop up. Or you see choice is None, and you read the exception statement returned in the error message and know NoneType is involved somehow. So then maybe an if not choices: raise conditional to catch for that?
The point I'm trying to make is the time you will spend trying to get this program to work like you want it to will spiral out of control. And if at the end you come up with some spaghetti code that does work like you want it to, something even worse will happen: you will have fixed a problem and not even know how. You have to learn something from the process. Take it from someone who's made this mistake for far too long: play stupid games, win stupid prizes. The prize of brute force debugging is wasted time and not being a better programmer for it.
Also know this about using debugging tools. They prevent you from writing more code to fix what's broken. Writing more lines of code is the solution maybe 1% of the time.
Concrete advice: Use breakpoints (the red dots you put by lines of code or in the line) to pause execution of the call stack where you want. The debugger window will show you all the variables inside that layer of the call stack, and you can use the debugging console like any other REPL to play around with values. ie. Change choice to 5 with choice = 5 then continue execution to see what happens. You can also use the call stack window to skip around the layers of code the interpreter runs. This way you can bounce in and out of functions and see what values are assigned where. Like, what is result in the if __name__ == '__main__': block in the myConfusingModule module?
Sorry this has gone so long. In the time it took you to read this you could have read the Visual Studio Code tutorial on debugging. Which if you haven't, do that now. Good luck.
1
u/backtickbot Mar 25 '21
1
1
1
1
u/EdwardWarren Apr 01 '21 edited Apr 01 '21
One of the best things to help with debugging since the invention of the print function is the AREPL extension in Visual Studio. It is beautiful, wonderful, fantastic, great, outstanding, genius, etc.
If you are not familar with it:
AREPL opens another screen that shows your data as soon as you type it in. It responds immediately to changes you make. I am terrible at explanations so let me demonstrate if I can.
Your program:
x = 3print (x)
AREPL:
3'Wow', you sarcastically say. How is that different from just running the code and seeing the print results?
It isn't but changes in the code happen immediately, right in front of you. If I change 3 to 4, AREPL changes to 4. Immediately. No running the code and looking in output for the answer. No stepping through your code, step by step using the debugger. AREPL has changed the way I code completely because it makes it possible to 'play around' with code and immediately see the results. If you code that way too, AREPL will be a great tool. Great tool for newbies. I temporarily set up a little function called prt in my code.
def prt(desc,*args):
print() #blank line
print(desc,*args)then use
prt('sub1: ', x) #sub1-identifier that highlights the line in AREPLin my code and the identified results show up cleanly and clearly in AREPL immediately.
Try it out.
16
u/SHKEVE Mar 24 '21
Learning to use breakpoints through tutorials really took my debugging to the next level.