search
HomeBackend DevelopmentPython Tutorialpycharm debugging tutorial

pycharm debugging tutorial

Dec 19, 2023 am 10:03 AM
pycharmpycharm debugging

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
Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python vs. C  : Memory Management and ControlPython vs. C : Memory Management and ControlApr 19, 2025 am 12:17 AM

Python and C have significant differences in memory management and control. 1. Python uses automatic memory management, based on reference counting and garbage collection, simplifying the work of programmers. 2.C requires manual management of memory, providing more control but increasing complexity and error risk. Which language to choose should be based on project requirements and team technology stack.

Python for Scientific Computing: A Detailed LookPython for Scientific Computing: A Detailed LookApr 19, 2025 am 12:15 AM

Python's applications in scientific computing include data analysis, machine learning, numerical simulation and visualization. 1.Numpy provides efficient multi-dimensional arrays and mathematical functions. 2. SciPy extends Numpy functionality and provides optimization and linear algebra tools. 3. Pandas is used for data processing and analysis. 4.Matplotlib is used to generate various graphs and visual results.

Python and C  : Finding the Right ToolPython and C : Finding the Right ToolApr 19, 2025 am 12:04 AM

Whether to choose Python or C depends on project requirements: 1) Python is suitable for rapid development, data science, and scripting because of its concise syntax and rich libraries; 2) C is suitable for scenarios that require high performance and underlying control, such as system programming and game development, because of its compilation and manual memory management.

Python for Data Science and Machine LearningPython for Data Science and Machine LearningApr 19, 2025 am 12:02 AM

Python is widely used in data science and machine learning, mainly relying on its simplicity and a powerful library ecosystem. 1) Pandas is used for data processing and analysis, 2) Numpy provides efficient numerical calculations, and 3) Scikit-learn is used for machine learning model construction and optimization, these libraries make Python an ideal tool for data science and machine learning.

Learning Python: Is 2 Hours of Daily Study Sufficient?Learning Python: Is 2 Hours of Daily Study Sufficient?Apr 18, 2025 am 12:22 AM

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Python for Web Development: Key ApplicationsPython for Web Development: Key ApplicationsApr 18, 2025 am 12:20 AM

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python vs. C  : Exploring Performance and EfficiencyPython vs. C : Exploring Performance and EfficiencyApr 18, 2025 am 12:20 AM

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)