Home  >  Article  >  Backend Development  >  pycharm debugging tutorial

pycharm debugging tutorial

尊渡假赌尊渡假赌尊渡假赌
尊渡假赌尊渡假赌尊渡假赌Original
2023-12-19 10:03:511995browse

The pycharm debugging tutorial is as follows: 1. Preparation work; 2. Run the code; 3. Enter Run/Debug mode; 4. Save run/debug configuration information; 5. Official operation; 6. Run test program; 7 , select a tester; 8. Create a test program block; 9. Run the test code; 10. Debug the run; 11. Breakpoint; 12. Set breakpoint 13. Code debugging; 14. Run again; 15. REPL, in Console interface debugger.

pycharm debugging tutorial

# Operating system for this tutorial: Windows 10 system, Dell G3 computer.

The pycharm debugging tutorial is as follows:

1. Preparation

The Python version is 2.7 or higher

A Python project has been created and added Content, specific reference: Getting Started tutorial

2. Step 1 - Run the code

Open the Solver.py file written before, right-click in the edit box and select " Run 'Solver'" option.

At this time, the script file runs normally and displays the output value of the program in the debugging tool window:

Next, we will give a detailed explanation of the specific contents of these two steps.

3. What is Run/Debug mode

Each script file that needs to be run/debugged requires a special configuration file to specify its script name, directory and other important running and debugging information. Pycharm has integrated this configuration file to avoid users having to create it manually. Every time you click the Run or Debug button (or perform the same operation in the shortcut menu), you actually load the current run/debug configuration file into the current debug model. If you look carefully at the first picture, you will find that there is no run/debug related information in the combo box at all, and they do not appear until the second picture. This means that the run/debug configuration file of the Solver script will be automatically generated when the run/debug command is executed, as shown now. At this time, the two buttons Run (green arrow button) and Debug (green beetle button) become available in the main toolbar:

At the same time, these two icons are still translucent, which means they are temporary. That is, automatically created by Pycharm. OK, click the drop-down arrow to view the currently available command operations:

If you have set up multiple run/debug configuration schemes, they will all be displayed in the drop-down list here. Click to select one as the current project. run/debug configuration file.

4. Save the run/debug configuration information

In the drop-down list above, click the Edit configuration option to open the run/debug configuration editing window:

On the left Two nodes will appear in the side directory: Python and Default. There is a single configuration option 'Solver' under the first node directory, and under the second option there are many configuration information.

What does this mean?

Under the Default node, you can only see the name of the framework or the name of the mode. If you create a new Run/Debug configuration file, it will be created under the selected mode branch. If you If the settings under the Default node are changed, all configuration files related to it will be changed accordingly.

For example, if you want to replace the Python interpreter used in Pycharm with a remote or local interpreter, you can change the interpreter settings under the Python page so that all new debugging configuration files will use this new one. interpreter.

Under the early Python node, only a single configuration option 'Solver' is used. It is a Python type configuration, but it is different from the Python mechanism under the Default node. It is represented by a non-transparent icon. , which is used to indicate the saving status of the current configuration file. When you save the configuration file, the icon will become non-transparent. For example, we create a new configuration file for the current Solver script under the Python type and name it 'Solver1'.

If you make any changes to an existing configuration file, these changes will only be applied to the corresponding script area.

5. Official operation

We have been able to run the script in a very direct way, and then we will look for other ways to run the script.

As we know, running a script means loading the current debugging configuration file, therefore, running the script mainly follows the following process:

(1) In the main toolbar, click run/ debug group box, confirm the current debugging configuration file information

(2) Do the following work (choose one of three):

Click the run button to load the configuration file

Press the Shift F10 shortcut key

On the main menu, select Run → Run

At this time, we can observe the running results of the program in the Run tool window.

6. Run the test program

We are not discussing the importance of code testing here, but exploring how Pycharm can help us complete this function.

7. Select a tester

First, you need to specify a tester. Click the Settings button on the toolbar to open the Settings/Preferences dialog box, then click to enter the Python Intergated Tools page (can be found through the search function). The default selection is as follows:

Here we select Nosetests, save and close dialog box.

8. Create a test program block

First we create a test instance. Pycharm provides a very smart way to create test code: click to select the class name and then press the Ctrl Shift T shortcut key, or select Navigate → Test in the main menu. If the test program already exists, it will jump directly to Corresponding code, otherwise create it:

Follow the system prompts, Pycharm will display the following dialog box:

Click the OK button to view the creation result: A test class has been automatically created. Of course, this is just a class framework and we need to manually write the test function.

9. Run the test code

After everything is ready, right-click the test class name and select the run command in the pop-up shortcut menu:

Observe the Test Runner in the running status bar Tab output:

10. Debugging and running

First of all, we need to figure out why we need to debug? Suppose our program hits an error during operation, how do we locate the location where the error occurs? This requires debugging.

In Pycharm, we can directly debug the program. The only preparation required is to add breakpoints where necessary in the program. Next, we will introduce it in detail:

11. What is a breakpoint?

A breakpoint marks the position of a line. When the program runs to that line of code, Pycharm will temporarily suspend the program to facilitate our analysis of the running status of the program. Pycharm supports several types of breakpoints, which can be distinguished by the corresponding icons. Here we use Python's line breakpoints as an example to introduce.

12. Setting breakpoints

The method is very simple, just click on the blank gray slot on the left side of the code:

Note that breakpoints will mark the corresponding lines of code as Red, this color mark cannot be changed by users yet, and we will introduce a solution as soon as possible.

By the way, canceling the breakpoint is also very simple, just click again at the same position.

When you hover the mouse pointer over the breakpoint, Pycharm will display the key information of the breakpoint, line number and script properties. If you want to change the properties of the breakpoint, right-click the breakpoint:

You can try to make personalized changes to the breakpoint properties, and then observe the changes in the icon.

13. Code debugging

Next, we officially start debugging the code.

First select the 'Solver' file with the same name from the configuration file group box as the current debugging configuration file, and then click the debug button (green beetle-style button):

Next will be Pycharm The following operations will be performed:

(1) PyCharm starts running and pauses at the breakpoint

(2) The line of code where the breakpoint is located turns blue, which means that the PyCharm program process has reached the breakpoint , but the code marked by the breakpoint has not yet been executed.

(3) The Debug tool window appears, showing the current important debugging information and allowing the user to make changes to the debugging process.

Although the Pycharm user manual has fully provided functional information on all controls in the debugging window, we still briefly introduce them here. We find that the window is divided into two tabs: Debugger tab and the Console tab.

(1) The Debugger window is divided into three visible areas: Frames, Variables, and Watches. These windows list the current frame and running processes, allowing users to view the status of variables in the program space, etc. When you select a frame, related variable information will be displayed. Of course, these areas can be folded and hidden.

(2) The Console window displays the current console output information. By default, this window is located under the Debugger and can be displayed in front by clicking its label.

Of course we can change the placement of these windows, if you don't like the default layout of the program. See the Moving tabs and areas chapter for details.

Working mode of the Debugger window:

OK, now the program is suspended at the first breakpoint, and the Frames window displays the process demo corresponding to the 7th line of code in the Solver script, related Variables a, b, c have been defined, but variable d has not yet been defined. Next?

Press F9 (or the green arrow on the left toolbar), and the program will continue to run to the next breakpoint. In this way, you can run each breakpoint again and observe the changes in variables. .

For more information about the Debugger window, see the software manual: product documentation

Working mode of the Console window:

Why do you need to use the Console window? When we need to view the error information given by the program, or perform some additional temporary operations, we need to do it in this window.

Click the Console tab to bring it to the front:

Then click the command button in the left toolbar to display the Python command prompt:

At this time Activate the console mechanism and try to execute some Python commands in it:

Note that the console window provides code spelling prompts (Ctrl Space) and historical memory (Up/Down keys) functions. For more information, see : Using Debug Console

Finally, if you want the Console window to be always available, just move it into a separate window:

14. Run again

After completing this debugging run and loading the debugging configuration file again, we can run debugging again by clicking the run button on the toolbar.

15. REPL - Debugging the program in the console interface

Finally, if you are more accustomed to working in a console environment, you can also set Pycharm to console mode. Select Tools → Run Python Console... in the main menu to load the console:

The console window will be activated and displayed as a separate window:

In this control We can do a lot of interesting things in the console window. Next we demonstrate how to import the code in the recently written Solver.py file into the console:

Open the Solver.py file (there are many ways to open it) , such as Ctrl E - View → Recent Files), select all the code content in the file (Ctrl A, or Edit → Select All), then press Alt Shift E (or right-click and select Execute Selection in Console in the pop-up shortcut menu ):

At this time, Pycharm will automatically import the selected code into the console interface so that we can edit it:

The above is the detailed content of pycharm debugging tutorial. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn