Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyCharm: debugging line by line?

Tags:

python

pycharm

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?

like image 993
user40780 Avatar asked Jun 07 '14 19:06

user40780


People also ask

How do I go to the next line in PyCharm debugger?

Steps over the current line of code and takes you to the next line even if the highlighted line has method calls in it. If there are breakpoints in the called methods, they are ignored. From the main menu, select Run | Force Step Over or press Alt+Shift+F8 .


1 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.

like image 78
Tiro Avatar answered Sep 19 '22 14:09

Tiro