Pycharm the basics of getting Started with code debugging

Source: Internet
Author: User
This article is mainly for you to introduce the most comprehensive pycharm learning tutorial The third code run debugging, with a certain reference value, interested in small partners can refer to

Pycharm code run debugging, the specific content is as follows

1. Preparatory work

(1) Python version is 2.7 or higher

(2) A Python project has been created and content has been added, for specific reference: Getting Started Tutorial

2. First step-run code

Open the previously written solver.py file, right-click in the edit box, and select the "Run ' Solver '" option on the shortcut menu.

The script file runs correctly and displays the program's output values in the Debug tool window:

Next, we explain the details of the two-step operation.

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, the directory in which it is located, and other important running debugging information. Pycharm has integrated this configuration file to prevent users from creating it manually.

Each time you click the Run or Debug button (or perform the same action on the shortcut menu), you are actually loading the current run/debug configuration file into the current debug model.

If you look closely at the first picture, you will find that there is no information about run/debug in the combo box and that they appear in the second picture. This means that the Run/debug configuration file for the Solver script is automatically generated when the Run/debug command is executed, as shown now.

Now the main toolbar is run (green arrow button) and debug (Green Beetle button) two buttons become available:

At the same time these two icons are still translucent, which means that they are temporary, that is, created automatically by Pycharm.

OK, click the drop-down arrow to view the currently available command actions:

If you have set up multiple run/debug configurations, they will be displayed here in the drop-down list, click to select a Run/debug profile as the current project.

4. Save Run/debug configuration information

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

There will be two nodes in the left directory: Python and default. There is a single configuration option ' Solver ' under the first node directory and a lot of configuration information under the second option.

What does that mean?

Under the default node, you can only see the name of the frame or the schema name, if you create a new Run/debug profile, it will be created under the selected mode branch, and if you change the settings under the default node, all the corresponding configuration files associated with it will change.

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 debug profiles will use the new interpreter.

Early Python node, only with a single configuration option ' Solver ', which is a Python-type configuration, but not the same as the Python mechanism under the default node, it uses a non-transparent icon for the representation, which is used to indicate the current configuration file save state, When you save the configuration file, the icon becomes non-transparent. For example, we create a new configuration file for the current Solver script under the Python type named ' Solver1 '.

If you make any changes to a configuration file that already exists, those changes will only be applied to the corresponding script area.

5. Official operation

We've been able to do this in a very straightforward way, and then we're looking for other ways to run the script.

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

(1) In the main toolbar, click the Run/debug group box to confirm the current debug profile information

(2) Do the following work (choose one of the 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 point, we can observe the running results of the program in the Run tool window.

6. Run the test program

We don't discuss the importance of code testing here, but rather explore how pycharm can help us do this.

7. Select a test device

First, you need to specify a tester. Click the toolbar's Settings button to open the Settings/preferences dialog and click on the Python intergated tools page (which can be found through the search function), with the default selection as follows:

Here we select Nosetests, save and close the 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 press ctrl+shift+t shortcut, or select Navigate→test in the main menu, if the test program already exists, it will jump directly to the corresponding code, otherwise create it:

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

Click the OK button to view the creation result:

At this point Pycharm has automatically created a test class, which is just a class framework that requires us to write the test function manually.

9. Run the test code

When you're ready, right-click the test class name and choose Run command from the popup shortcut menu:

Observe the output of Test Runner tab in the running status bar:

10. Commissioning and operation

The first thing to figure out is, why debug? Assuming that our program has hit an error during operation, how do we locate the error where it occurred? This is required for debugging.

In Pycharm in which we can directly debug the program, the only thing we need to do is to set breakpoints where the program is necessary, then we will introduce in detail:

11. What is a breakpoint?

A breakpoint marks the location of a row, and when the program runs to that line of code, Pycharm suspends the program temporarily to facilitate our analysis of the program's running state. Pycharm supports several types of breakpoints types of breakpoints, which can be distinguished by corresponding icons.

Here we use the Python line breakpoint as an example to introduce

12. Set Breakpoints

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

Note that the breakpoint marks the corresponding line of code as red, the color tag is not yet changed by the user, and we will introduce a solution as soon as possible.

By the way, it is also easy to cancel a breakpoint and click again in the same location.

When you hover the mouse pointer over a breakpoint, Pycharm displays the key information for the breakpoint, the line number, and the Script property, and if you want to change the breakpoint's properties, right-click the breakpoint:

You can try to personalize changes to the breakpoint properties, and then observe the icon changes.

13. Code Debugging

Next, we formally start debugging the code.

First select the ' Solver ' file with the same name as the current debug profile from the Configuration file group box, and then click the Debug button (green Beetle-style button):

Next, Pycharm will do the following:

(1) Pycharm starts to run and pauses at break point

(2) The line of code in which the breakpoint is located is blue, meaning that the Pycharm program process has reached the breakpoint, but the code marked by the breakpoint has not been executed.

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

Although the functionality information for all the controls in the Debug window has been fully provided in the Pycharm user manual, we will still give a brief introduction to them here. We found a window 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, the running process, and make it easier for the user to see the status of variables in the program space. When you select a frame, the relevant variable information is displayed, of course these areas can be collapsed and hidden.

(2) The console window displays the current console output information, which, by default, is located below debugger and can be displayed by clicking its label.

Of course we can change the placement of these windows, if you do not like the default layout of the program. Specifically participate in moving tabs and areas chapters.

Working mode of the debugger window:

OK, now that the program is paused at the first breakpoint, the Frames window shows the process demo for the 7th line of code in the Solver script, the related variables A, b, and C have been defined, but the variable D has not yet been defined. Next?

By pressing F9 (or the green arrow on the left toolbar), the program will continue to run to the next breakpoint, in which case you can run each breakpoint again and observe the variable.

For more information about the debugger window, see the Software manual: product documentation

Operating mode of the console window:

Why do I need to use the console window? When we need to look at the error message given by the program, or do some extra temporary operations, we need to do it in this window.

Click the Console tab to make it front-facing:

Then click on the command prompt in the left-hand toolbar to display the Python commands:

At this point, the console mechanism is activated, where you try to execute some Python commands:

Note that the Console window provides spelling hints for the code (CTRL+SPACE) and the Historical memory (Up/down keys) feature, for more information see: Using the Debug console

Finally, if you want the console window to remain available, simply move it into a separate window:

14. Run again

After you have completed the debug run and loaded the Debug configuration file again, we can run the debugging again and click the Run button on the toolbar.

15, repl--in the Console interface debugging program

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

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

In this console window we can do a lot of interesting things, and then we show how to import the code from the recently written solver.py file into the console:

Open the solver.py file (there are a variety of open methods, such as ctrl+e-view→recent files), select the code content in the file (Ctrl + A, or edit→select all), and then press alt+shift+ E (or right-click on the popup shortcut menu to choose Execute Selection in Console):

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

"Recommended"

1. Python Free video tutorial

2. Python Learning Manual

3. Python Object-oriented video tutorial

Related Article

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.