I am using PyCharm (community version) for my Python IDE. I want the program to debug in a line-by-line fashion. So I don't want to set every line as a break point... Is there a way I could do this?
4 Answers
As @Cyber mentioned, the debugging hotkeys will let you step through line by line, step down into function calls, etc., once you've hit a breakpoint and stopped somewhere.
If you really want to step through each line, you could set a breakpoint somewhere at the very beginning of your code. If you're using a main() function in your code, e.g.:
def main():
....
if __name__ == '__main__':
main() # Breakpoint here, 'Step Inside' to go to next line
then you could set the breakpoint at the call to main()
. (If you're not, you might want to try this approach.)
One other thing I'd point out is PyCharm's easy-to-overlook feature of conditional breakpoints. If you right-click on the breakpoint symbol in the gutter area of the editor, you can type in a condition, like n > 10
; the breakpoint only triggers when that line is executed and the condition is met. When you're trying to debug code issues within a recursive function, say, this can simplify things a lot.
I know the last part isn't really what you were asking for, but as your codebase gets bigger, going through each line can get really time consuming. You'll probably want to focus more on things like unit testing and logging with larger projects.
To run in debug mode press the 'bug' button (or Shift + F9
).
Step over - F8
Step into - F7
Step out - Shift+F8
.
Step to next breakpoint (or end) - F9
-
2Good, but can one step one line forward without setting a new breakpoint? Apr 6, 2021 at 8:29
-
Step over (F8) makes you move to the next line. And if you're not on a function call, Step into (F7) achieves the same.– tvoAug 16, 2021 at 15:10
The pdb module only needs 2 lines of code in your program to be able to step through line by line.
import pdb # Insert this as the first line of your program
pdb.set_trace() # Insert this once in your program to step through it
# code you want to step into
This youtube video explains everything in 6 mins.
step over
functions,step into
, etc.