- Make sure that you are working with PyCharm version 3.0 or higher. If you still do not have PyCharm, download it from this page. To install PyCharm, follow the instructions, depending on your platform.
- You have at least one Python interpreter installed and configured for your project.
- You have already created a pure Python project.
- You are working with an example, used in the Debugger tutorial.
PyCharm discerns several types of breakpoints, each one denoted with its own icon. In this tutorial, you'll deal with the Python line breakpoints - the lines of the source code, where PyCharm will suspend, when such a line is reached.
There are two ways of viewing and changing breakpoint properties in PyCharm:
- First, there is a single breakpoint properties pop-up window.
- Second, there is Breakpoints dialog that shows all the breakpoints existing in an application, with their properties.
Choose a line where a brekpoint should be set, and do one of the following:
- Click the left gutter.
- Press Ctrl+F8
This action is a toggle - repeat it to remove an existing breakpoint, and then set it again.
In this exercize, let's place the breakpoint at the line 16:
To view details of a single breakpoint, right-click it:
First, clear the check box Line 16 in ThreadSample.py enabled. Thus you will disable the breakpoint - its icon now changes to :
OK, return this check mark back to enable the breakpoint. The icon changes to .
Suppose now, you want to PyCharm to suspend for one thread only, and pass for the other. Most simpe - add a condition to the breakpoint:
Then, when you start the debugger session ( with the run/debug configuration 'ThreadSample'), you will see that PyCharm actually pauses for Thread-7, but passes the breakpoint for Thread-6:
Normally, you just need to suspend at a breakpoint, and that's all... But what if, for example, you don't need PyCharm to suspend at a breakpoint, but still need something else - for example, logging ?
Clear the check box Suspend - the breakpoint properties dialog shows now much more fields:
Select two check boxes:
- Log messages to console - when you select this check box, then PyCharm will write to the console the important information about the reached breakpoint.
- Log evaluated expressions - when you select this check box, PyCharm will evaluate the specified expression on-the-fly and write values to the console.
By the way, mind code completion (Ctrl+Space) in the evaluate expression field. OK, now launch debugging again, and observe the console output. As you can see, PyCharm writes to the debugger console trifold information:
- Messages stipulated by the application
- Log information about reaching a breakpoint
- Results of an expression evaluation
The dialog you have used so far is but a shortcut that enables you to specify the most necessary properties of a breakpoint. However, the usual way of dealing with the breakpoints in an application is to use the Breakpoints dialog. It shows all the breakpoints of all types, which exist in your application, and makes it possible to change their settings.
You can invoke this dialog in multiple ways:
- First, press Ctrl+Shift+F8.
- On the main menu, choose Run → View Breakpoints
- Click the link More (Ctrl+Shift+F8) in the breakpoint properties pop-up dialog.
- When in the Debug tool window, just click the button in the toolbar of the tool window.
Suppose, you want to delete a breakpoint after it has been hit - PyCharm suggests a special check box Remove once hit. By default, it is cleared. Provided that the Suspend check box is selected, select also the check box Remove once hit. You see that the breakpoint icon has changed to :
Launch the debugger again - this time PyCharm pauses only once at the specified breakpoint, and removes it after that, so that the further execution goes on without any stops:
In this tutorial you have:
- Refreshed your memories on the notion of a breakpoint
- Noted the difference between the dialogs showing properties of a single breakpoint and all breakpoints in an application.
- Learnt how to set a condition for a breakpoint
- Learnt how to log messages to console
- Learnt how to make a breakpoint temporary.