In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what are the preparations for the work of the Pycharm debugger". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Preparatory work
(1) Pycharm version 3.0 or higher
(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 through Pycharm, the role of each tool button, and so on. For Python programming methods, see Python documentation.
3. Knowledge to be reviewed
In order to complete this tutorial, you need to use the first two important knowledge points:
(1) knowledge of Run/debug configuration files
(2) knowledge of breakpoints
4. Run/debug configuration knowledge
Whenever you run or debug a script file through Pycharm, you need a special file to record the name of the script, the working directory, and other important debugging information. Pycharm has pre-created configuration files for specific patterns, preventing us from creating them manually.
Every time you click the run or debug button (or do the same thing through the shortcut menu), we actually load the corresponding configuration file in the current working mode. See product documentation for details.
5. Breakpoint
A breakpoint marks a line of code that suspends the program temporarily when Pycharm runs to that line of code. Pycharm provides several forms of breakpoint types of breakpoints, each with a different icon icon. For more information, see Breakpoints tutorial in product documentation.
6. Examples
In your Python project, create a new Python file named ThreadSample.py, and enter the following code:
7. Set breakpoints
First, set a breakpoint in the source code. Generate a breakpoint at the corresponding location by clicking the blank slot on the left side of the code:
8. Start debugging
Select run/debug configuration "ThreadSample", then press Shift+F9 (or click the green spider button in the toolbar) to start debugging and stop at the first breakpoint:
The line on which the breakpoint is located turns blue, indicating that Pycharm has hit the breakpoint, but the line of code has not been executed.
9. Change the layout of the debugging window
At this point, Pycharm enters the debug interface mode Debug tool window. For more information on the specific functions of each control, please see know how to use this tool window.
Add that you don't like the current default layout, for example, if you want to display the debugger output window as a separate window to make it easier to observe the current debugging state, you can customize the layout.
First, we separate the debug tool window, just right-click the title bar of the window, and then select Floating mode:
Next, move the console window to a separate window, just drag the console window and drag it out of the test window:
You can revert to the default layout by clicking the button on the toolbar.
For more information, see PyCharm Tool Windows and Moving tabs and area
10. Add a variable viewer
Next, we'll show you how to observe the state of variables during debugging. We need to set up a viewer for it. In the Watches window, click the green plus sign, enter the name of the variable you want to view, for example, enter delay here, and enter enter. Of course, you can also use another way: right-click the variable name in the editing window and select Add to watches from the shortcut menu:
At this point, observe the Watches window and find that the delay variable is not currently defined:
Later you will see how to negative this variable and how it changes in the watches window. Next we set up a viewer for the get_thread_name () function as an exercise.
11. Browse the frame
At this point you can see a process called MainThread, which contains three frames. Click each frame to display its variable state and the corresponding py file, and highlight the line of code in question:
12. Simple debugging
When the button is clicked at each breakpoint, the program continues to run and observe the script output from the console:
13. Step by step script debugging
Let's further debug the current script. Click to restart the debugging process, and the program runs to the first breakpoint again and pauses.
At the top of the debug window, you will see a list of toolbar stepping buttons that contains step debug buttons.
Click, or press F8, and you will find that the blue mark 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 (), and you can select any of these processes and observe the changes in the variables:
14. Run to the current cursor
What if you don't want to add breakpoints, but still want the code to debug and run to a specified line of code?
Return to the second breakpoint of the debugging interface and insert the input cursor in the corresponding row.
Click the button, or press the Alt+F9 shortcut, and the line of code becomes highlighted:
15. How to invoke Debug commands
It is worth mentioning that all debugging operations can be accomplished not only by debugging the corresponding buttons of the 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 shortcuts:
This is the end of the content of "what are the preparations for the work of the Pycharm Debugger"? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.