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

Source: Internet
Author: User

Most complete Pycharm Tutorials (1)--Custom Skins

Most full Pycharm Tutorials (2)-code style

Most full Pycharm Tutorials (3)-code debugging, running

Most complete Pycharm Tutorials (4)--related configuration for Python interpreter

Most full Pycharm Tutorials (5)--python shortcut key Related settings

Most full Pycharm Tutorials (6)--using Pycharm as a VIM editor

Most full Pycharm Tutorials (7)--Configuration of VM VMs

Most complete Pycharm tutorials (8) creation and management of--django engineering

Most complete Pycharm Tutorials (9)--Create and run a basic Python test program

1. Preparatory work

(1) Pycharm version 3.0 or later

(2) At least one Python interpreter is installed and configured correctly

(3) A Python project has been created

2. Main content

Describes how to debug script files with Pycharm, as well as the role of individual tool buttons, and so on, see Python documentation for a python programming approach.

3, to review the knowledge

In order to complete this tutorial, we need to use the previous two important points of knowledge:

(1) Knowledge of run/debug configuration files

(2) Related knowledge of breakpoints

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, enter the name of the variable you want to see, 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:

You will see later how to make a negative value on this variable and its change 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:

12. Simple Commissioning

When you click the button on each breakpoint, the program continues to run, 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.

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

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.