[Python] Debugger in Pycharm

Source: Internet
Author: User

From : http://blog.csdn.net/u013088062/article/details/50214459

from:http://blog.csdn.net/u013088062/article/details/50216015

from:http://blog.csdn.net/u013088062/article/details/50221825

Most complete Pycharm tutorial (Ten)--pycharm Debugger General Chapter

Most complete Pycharm tutorial (one)--pycharm Debugger Breakpoint Chapter

Debug settings

Debugging process

Set breakpoints

Conditional control Breakpoints

Debug Settings

4, Run/debug configuration knowledge

Whenever you run through Pycharm or debug a script file , you need a special file to record the name of the script, the working directory, and other important debugging information . Pycharm has pre-created the configuration file for a specific pattern, so we don't have to create it manually.

Each time you click the Run or Debug button (or perform the same action via the shortcut menu), we are actually loading the corresponding configuration file in the current working mode . See product documentation.

5. Breakpoints

  A breakpoint is marked with a line of code , and the program is temporarily suspended when Pycharm runs to that line of code. Pycharm provides several forms of breakpoint types of breakpoints, and its icon is different. See the Breakpoints Tutorial in product documentation.

6. Example

In your Python project, create a new Python file named threadsample.py, and then enter the following code:

  

7. Set Breakpoints

First, set breakpoints in the source code. Generate a breakpoint at the corresponding location by clicking the blank slot to the left of the code:

  

8. Start Commissioning

Select Run/debug Configuration "Threadsample", and then press shift+f9(or click the Green Spider button in the toolbar), start debugging, and stop at the first breakpoint:

  

The line that contains the breakpoint changes to blue, indicating that Pycharm has hit the breakpoint but has not yet executed the line of code.

9. Change the layout of the debug window

At this point pycharm into the Debug Interface Mode Debug Tool window, the specific functions of the various controls are described in know how to use the This tool window.

Join you do not like the current default layout, for example you want to display the debugger output window as a separate window to make it easier to observe the current debugging state, you can personalize the layout.

First, we first separate the Debug tool window, just right-click the window's title bar and select Floating mode:

  

Next, move the console window into a separate window, just drag the console window to drag it out of the test window:

  

You can revert to the default layout by clicking the buttons on the toolbar.

For more information, see Pycharm Tool windows and Moving tabs and area

10. Add a variable viewer

Next we show you how to observe the state of the variables during debugging. We need to set a viewer on it. In the Watches window, click the green plus sign to enter the name of the variable you want to view.

For example, enter delay here and then enter. Of course you can also use another way: Right-click the variable name in the edit window and choose Add to watchesfrom the shortcut menu:

Now look at the watches window and find that the delay variable is not currently defined:

  

Later you will see how to assign the variable and its changes in the Watches window. Next we set up a viewer for the Get_thread_name () function as an exercise.

11. Browse Frames

At this point you can see a process called Mainthread , which contains three frames. Click each frame to display its variable status and the corresponding py file , highlighting the problematic line of code:

  

Debugging Process

12. Simple Commissioning

Click the button at each breakpoint to keep the program running, observing the console's script output:

  

  

13. Stepping Script debugging

Let's further debug the current script. Click Restart Debugging process and the program runs again to the first breakpoint and pauses.

At the top of the debug window, you will see a list of toolbar stepping buttons with the Step Debug button.

Click, or press F8, and you'll notice that the blue tag moves to the next line :

  

At the same time, when you pause the execution of the script (click the button), you can see the highlighted function Print_time (), you can select any one of these processes and observe the change of the variable:

  

14. Run to the current cursor  

If you do not want to join the breakpoint but still want the code to be able to debug and run to a specified line of code, how do I do it?

Returns the second breakpoint at the debug interface, inserting the input cursor at the corresponding line.

  

Click the button, or press the alt+f9 shortcut key to change the line code to highlight:

  

15. How to invoke the debug command

It is worth mentioning that all debugging operations can be done not only by the corresponding buttons on the Debug toolbar, but also by commands under the Run menu in the main menu, as well as related shortcut menu items. Here are some common Debug menu commands and their corresponding list of shortcut keys:

  

For more control descriptions See Debug Tool window.

Set Breakpoints

3. Breakpoint Properties

There are two ways to browse and change code properties:

(1) First, there is a separate pinned window to display the breakpoint properties.

(2) There is a breakpoint dialog breakpoints dialog to display all breakpoints and their properties that exist in the current app.

4. Set Breakpoints

Select the corresponding line of code, and then perform the following steps (two options):

(1) Click left blank slot

(2) Press the Ctrl+f8 shortcut key

This is a trigger-switch operation that can be repeated repeatedly to remove/add breakpoints.

As an exercise, set a breakpoint on line 16th:

  

5. Browse and Change breakpoint properties

To explore the properties of a single breakpoint, just right-click it :

  

activating Breakpoints

6. Enable and Close breakpoints

First, clear the line in threadsample.py enabled option in the check box, at which point the breakpoint is invalidated and the center of the icon changes to green:

  

OK, tick the checkbox again, the icon reverts to its original state, and the breakpoint takes effect.

Conditional Control Breakpoints

7, conditional code hanging

Suppose you want Pycharm to suspend only one thread at the current code, while other threads can pass the breakpoint normally , it is very simple to add a condition to the breakpoint:

  

Next we start the Script debugging (click the button in the toolbar) and you'll see that the pycharm actually suspends the Thread-7 at the current breakpoint, and Thread-6 normally passes the breakpoint:

  

8, how to ignore some breakpoints?

In general, you just need to stop at one breakpoint and expect it to pass at a different breakpoint.

In other words, we don't want the pycharm to stop at the breakpoint, but we still need to keep the corresponding breakpoint as a token, how do we do it?

In the dialog box, uncheck the Suspend check box, and a more detailed message appears in the Breakpoint Properties dialog box:

  

Tick the two options:

(1)Log messages to console: When this option is selected, Pycharm displays relevant important information on the console after the breakpoint is hit.

(2)Log evaluated expressions: When this option is selected, Pycharm calculates the specific expression given in the input box below and displays it on the console.

  

By the way, Pycharm also provides spelling hints (CTRL + SPACEBAR) When you enter an expression in the input box. OK, rerun debugging and Pycharm will display the message in the Debug console:

    • Basic information about the application.
    • Registration information When you click the breakpoint
    • The result of an expression

  

9. Browse All Breakpoints

All of the breakpoint-related dialog boxes you use above can help you get most of the important information about breakpoints. However, we usually use the Breakpoints Dialog dialog box to do most of the breakpoint processing, which displays the property information for all breakpoints in the current project and makes it easy for us to change it.

There are several ways to Open this dialog box :

    1.  ctrl+shift+f8 shortcut keys
    2. In the main menu, select Run→view Breakpoints
    3. In the Breakpoint Properties Shortcut dialog box, click More (CTRL+SHIFT+F8)
    4. In the Debug window, click the button.

10. Create a temporary breakpoint

Suppose you want such a breakpoint, that is, pycharm to delete the hit breakpoint after hitting it . Pycharm provides a special complex option for the Remove oncehit, which by default does not start the mechanism .

The Add Suspend check box is selected, and then the Remove once hit check box is selected to observe the change in the icon of the breakpoint:

  

Re-debug, it is found that the pycharm will only be paused at the breakpoint and then removed to ensure that subsequent program execution is not hindered:

  

[Python] Debugger in Pycharm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.