The most complete Pycharm debug skills, [recommended collection]

image

If a worker wants to do his job well, he must first sharpen his tools. Whether your IDE is IntelliJ IDEA, Pycharm, WebStorm, GoLang, or PhpStorm, the debugger is standard. When encountering a problematic program, reasonable use of the debugger's tracking and breakpoint skills can quickly locate the cause of the problem. Let us take Pycharm as an example, let's systematically learn IDE debugging skills.

01.Debug operation mode

Left-click on the line number where the code is located, and a line breakpoint will be set. Click the small bug icon button in the upper right corner of Pycharm, and the code will run in DeBug mode. Below is a brief introduction to the DeBug working interface.

02. Menu button function introduction

In normal Debug code, some commonly used debugging buttons are as follows, the blue font is the name of the button:

1).Resume Programe:

Resume the program, for example, if you have two breakpoints on lines 12 and 16, and currently run to line 12, press Resume Programe to run to the next breakpoint (line 16);

image

 

2).Step Over:

Go down line by line. If there is a method on this line, it will not enter the method. For example, the 17th line of code below has its own instance method. Clicking the Step Over button to debug the code will not enter this instance method.

image

3).Step into:

Mandatory step, you can enter any method, you can use this method to enter the official class library when viewing the underlying source code.


Many people learn python and don't know where to start.
Many people learn python and after mastering the basic grammar, they don't know where to find cases to get started.
Many people who have done case studies do not know how to learn more advanced knowledge.
For these three types of people, I will provide you with a good learning platform, free to receive video tutorials, e-books, and course source code!
QQ group: 721195303


4).Step Into My Code

For single-step entry, if the current line has a custom method, it will enter the method, but will not enter the method of the official class library. The effect is as follows:

image

 

03. Commonly used DeBug tips

1). Breakpoint condition debugging

There is such a scenario, when the code is DeBug, I want to know what the value of date_1 is when the index is 10 in the following code. Normally, the code DeBug loops 10 times, which is very inconvenient.

image

There is a conditional breakpoint in the IDE. You only need to set the variable condition. Only when a variable is equal to the value of the condition you set, the breakpoint will automatically stop. First, right-click the breakpoint on line 24, set the variable condition index == 10 for Conditon, and click Done to save the setting.

When the value of index is 10, DeBug will automatically stop, the effect is as follows:

2). Calculation expression

When we debug a certain method, sometimes we forget to pass in a certain parameter or pass it wrong. At this time, we don’t want to debug again, we can set the value of a certain variable by calculating the expression, as shown in the figure below, we find that day_int = 367 is not the expected value, you can right-click the blue day_int below, select Set Value, and temporarily modify the value of day_int

The following code program will run down according to the re-assigned value, and you can see that the value of the variable num_30 is 200 divided by 30.

3). Multi-threaded debugging

Sometimes it is found that it is impossible to debug multi-threaded concurrent code, and not all breakpoints are reached. The variable in args in line 87 of the code gives 6 values, the number of concurrent threads is set to 3, and the breakpoint on line 71 Debugging I only cycled twice, normally it should be cycled 6 times, as shown below:

image

The above situation occurs because the IDE's default blocking level is Thread during Debug. The solution is to change its blocking level to All to block other threads. Only when the current debugging thread is finished will other threads go. The configuration method is to right-click the breakpoint, change the value of Suspend to All, and then click Make Default, preferably click Done to save the settings.

The effect of running after modification is as follows:

image

 

The debugging skills that I worked hard to organize all night are shared here. It is not easy to be original. Welcome friends to leave a message in the comment area.

I still want to recommend the Python learning group I built myself : 721195303 , all of whom are learning Python. If you want to learn or are learning Python, you are welcome to join. Everyone is a software development party and share dry goods from time to time (only Python software development related), including a copy of the latest Python advanced materials and zero-based teaching compiled by myself in 2021. Welcome friends who are in advanced and interested in Python to join!

Guess you like

Origin blog.csdn.net/aaahtml/article/details/112977151