The most complete Pycharm tutorial (3)--code debugging, running

Source: Internet
Author: User

Pycharm Tutorial (1)--Customizing the appearance

Most full Pycharm Tutorials (2)--code style

1. Preparation work

(1) Python version 2.7 or later

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

2, the first step-run the 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 Debugging Tools window:

Next we have a detailed explanation of the specific contents of the two-step operation.

3, what is the 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 run-time debugging information. Pycharm has integrated this configuration file to prevent users from manually creating it.

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 run/debug information in the combo box, and that they appear in the second picture. This means that when the Run/debug command is executed, the Run/debug configuration file for the Solver script is automatically generated, as shown now.

The Run (green arrow button) and debug (Green Beetle button) two buttons in the main toolbar become available at this time:

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

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

If you have set up multiple run/debug configuration scenarios, they will all appear 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 in the previous illustration, 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 ' in 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 frame name or schema name, and if you create a new run/debug configuration file, it will be created under the selected pattern branch, and if you change the settings under the default node, all of the corresponding configuration files will be changed.

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 the new interpreter will be used for all new debug configuration files.

Under the early Python node, only a single configuration option, ' Solver ', is a Python-type configuration, but not the same as the Python mechanism under the default node, which uses a non-transparent icon to indicate that the current configuration file is saved. 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 Python type, named ' Solver1 '.

If you make any changes to the existing configuration file, the changes will only apply to the corresponding script area.

5, the official operation

We've been able to do it 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 basically 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 (three select one):

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 results of the program in the Run tool window.

6. Run test program

We're not talking about the importance of code testing here, but about how pycharm can help us accomplish this.

7. Select a tester

First, you need to specify a tester. Click the toolbar's Settings button, open the Settings/preferences dialog box, and then click into the Python intergated tools page (which you can find by searching), and the default selection is 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 by clicking the class name and pressing the Ctrl+shift+t shortcut, or by selecting Navigate→test in the main menu, or by jumping directly to the corresponding code if the test program already exists, or creating it:

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

Click the OK button to view the results of the creation:

At this point Pycharm has automatically created a test class, of course, this is just a class framework, we need to manually write test functions.

9. Run Test code

When you are ready, right-click the test class name and select Run command from the pop-up shortcut menu:

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

10, commissioning and operation

First, figure out why you want to debug. Suppose our program hits a bug during the run, how do we locate where the error occurred. This requires debugging.

In Pycharm, we can debug the program directly, the only preparation that needs to be done is to set the breakpoint in the necessary place of the program, and then we will introduce it in detail:

11, what is a breakpoint.

A breakpoint marks the position of a row, and when the program runs to that line of code, Pycharm suspends the program temporarily so that we can analyze the running state of the program. Pycharm supports several types of breakpoints types of breakpoints, which can be distinguished by corresponding icons.

Here we take Python's line breakpoints for example

12. Set Breakpoints

method is 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, and that the color tag is not currently being changed by the user, and we will introduce a solution as soon as possible.

By the way, canceling a breakpoint is also simple, and clicking it again in the same position.

When you hover over a breakpoint, Pycharm displays key information about the breakpoint, line number, and script properties, and if you want to change the property of the breakpoint, right-click the breakpoint:

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

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 Profile group box, and then click the Debug button (green Beetle-style button):

Next, Pycharm will do the following:

(1) Pycharm starts running and pauses at breakpoint

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

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

Although the Pycharm use manual has provided complete functionality information for all the controls in the debugging window, we still have a brief introduction to it here. We found 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 framework, the running process, and the user to see the status of variables in the program space. When you select a frame, it will display the relevant variable information, of course, these areas can be folded hidden.

(2) The console window displays the current console output information, which is under debugger by default, and can be displayed 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. Specific participation in moving tabs and areas chapters.

The 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 the Solver script, the related variables A, B, 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 continues to run to the next breakpoint, in which case you can run each breakpoint and watch the variable change.

For more information about the debugger window See software manual: Product documentation

Working 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 ad hoc operations, we need to do it in this window.

Click the Console tab to make it predecessor:

Then click the command button on the left toolbar to display a command prompt for Python:

At this point, the console mechanism is activated, trying to execute some Python commands in it:

Note that the Console window provides code spelling hints (ctrl+space) and historical memory (Up/down keys) features, more information see: Using Debug Console

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

14. Run again

After completing this debug run and loading 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 the Pycharm to 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:

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

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

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



If you feel that this article has inspired you, please pay attention to my public number, I will be as active as possible to communicate with you, thank you.


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.