9.1 Starting the Debugger
We're gonna dive into debugging using PyCharm. It's logical and fun. You'll see for yourself.
In PyCharm, you can run your program in two modes:
Program Run Modes | Icon on the Panel | Hotkeys |
---|---|---|
Normal Execution | Shift+F10 | |
Run in Debug Mode | Shift+F9 |
You're familiar with the normal mode: your program runs, does its thing, and then terminates. Debug mode, though, can be full of surprises for you.
Debug Mode
Debug mode allows you to execute your entire program step by step. Or more accurately, line by line. You get to see the values of variables at each step (after executing each line of code). You can even change their values!
To master the art of debugging, you need to learn three things:
- Breakpoints
- Step Execution
- Viewing Variable Values
9.2 Breakpoints (Break Points)
The IDE lets you set special markers in your code — breakpoints (break point)
. Every time your program, running in debug mode, hits a line marked as a break point
, it'll pause.
To set a break point
, just click to the left of the line in PyCharm. Example:
The line gets marked with a breakpoint (break point)
, and PyCharm highlights the entire line in red:
Clicking again to the left of a line will remove the set break point
.
You can also set a break point
on the current line with the hotkey — Ctrl+F8. Pressing Ctrl+F8 again on a line with a break point
will remove it.
9.3 Running the Program in Debug Mode
If your program has at least one breakpoint, you can run your program in debug mode (Shift+F9 or the "bug icon").
When you start in debug mode, the program runs as usual. But when it hits a line marked with a break point
, it pauses. Example:
At the top half of the screenshot, you see the program code with two breakpoints. The program paused at line 2 — marked with a blue line
. Line 2 hasn't executed yet: nothing's been printed to the console.
At the bottom half of the screen, you see the debug mode panels: the Thread & variables
panel, the Console
(output), and a set of buttons for debug mode.
You can take your program off pause (resume its execution) by clicking the Resume Program
button at the bottom left panel (or press F9).
If you press this button (or F9), the program will run until it hits the next breakpoint or finishes. Here's what we'll see after pressing the button:
The program stopped at the second breakpoint, and the console shows the words "Hello"
and "and"
— indicating that out of three lines to be printed, only two have run so far.
9.4 Step Execution
In debug mode, you can also execute your program step by step: one step — one line. There are two hotkeys for step execution: F7 and F8: both will execute the current line of code. But first, you'll need to stop your program using a break point
.
If you want to execute your program line by line, place a break point
at the very start — the first line of code — and start it in debug mode.
When the program stops, you can start executing it line by line. One press of F8 — one line.
Here's what our program looks like after stopping and pressing F8 once:
The first line — print("Hello")
— has already run, and the current line is the second. You can also see in the screenshot's bottom part that the word "Hello"
has printed to the screen.
9.5 Step Execution with Functions
If you've got your own functions in the program, and you want to step through them during debugging, press F7 instead of F8 to "step into" the function.
Suppose you're stepping through the program and have stopped at line 5. If you press F8, PyCharm will just execute the fifth line and move to the sixth.
But if you press F7, PyCharm will step through the hello()
function:
It's all pretty straightforward. If you don't care much about what's happening inside a method, hit F8; if you do, press F7 and step through the entire code.
GO TO FULL VERSION