• About Us
King of Computer Media
  • Home
  • Tech News
  • AI News
  • Apps & Tutorials
  • Mobile & Telecom
  • Lifestyle
  • About Us
No Result
View All Result
  • Home
  • Tech News
  • AI News
  • Apps & Tutorials
  • Mobile & Telecom
  • Lifestyle
  • About Us
No Result
View All Result
King of Computer Media
No Result
View All Result

Home - Computer Applications and Other Tutorials - Debug Python programs using Visual Studio Code

Debug Python programs using Visual Studio Code

KOCPC Editor by KOCPC Editor
July 29, 2020 - Updated on August 4, 2026
in Computer Applications and Other Tutorials

Regardless of whether you are a senior programmer or a novice programmer, you will always run it after writing it, but it may not be executed as expected. Once this happens, finding out the cause of the problem is the only way for every programmer. Whether it is unfamiliarity with the grammar or logical problems, sometimes staring at the program code for hours still cannot find the crux of the problem. At this time, relying on debugging tools will get twice the result with half the effort. The VS Code tool supports multiple development languages ​​​​and Extensions. I still remember that the author installed it before Python extension for VS Code After expanding the package, it can easily become an IDE development environment. Let’s talk about this debugging environment and application.

用 Telegram 訂閱【挨踢路人甲】最新文章:https://t.me/itwshare

Install the Python extension for VS Code extension kit, and your Visual Studio Code will easily have IntelliSense, linting, debugging, code navigation, code formatting, Jupyter notebook support, refactoring, variable explorer, test explorer, snippets and many other functions. What is commonly encountered is the IntelliSense and Auto-Completion functions that are provided when writing Python. Although they are helpful for writing program syntax, they cannot fully display the problems and bugs of the program. At this time, the debugging environment must be used to gradually execute the program code to easily find the location of programming errors.

 

To enter a debugging environment, setting a breakpoint is a very important action, because this breakpoint is the position where your execution stops. Click on the line number on the left side of the figure below and a red Breakpoint will appear. This red point is the position where the debug execution will stop later.

 

Generally, the execution button in the green box on the upper right or the [CTrl + F5] shortcut key is used to execute the test program. Of course, you can also directly enter “python program name.py” on the terminal program screen. So how to start debugging? It’s very simple, just use the function representation [Run] → [Start Debugging]. The author always uses the [F5] shortcut key for convenience.

 

At this time, enter the Debug Configruation menu (it seems that Chinese appears after upgrading VS Code 1.47), select “Python file to detect the python file currently in use”.

At this time, the program will run to the break point you specified (blue box). In the red box are the quick buttons that can be executed in Debug, and in the green box you can see the values ​​of the variables. How come the variables i and j stop at i=4 and j=2? This is mainly related to the break point you specify. In the figure below, the break point is entered for the first time when i=4 and j=2, so the break point must be specified skillfully.

 

After the program entering the break point is run, the quick button aboveF5 (continue),F10(next line),F11(Enter),Shift + F11(jump out),Ctrl + Shift + F5(restart) andShift + F5(stop).

 

The author pressedF10 (Step Over) jumps to the next execution line. At this time, the i variable becomes 5, and the icon symbol table in the red box is the current execution position.

 

Press againF10 (Step Over) goes down to the if judgment.

 

Click againF10 (Step Over) does not stop at the break point. That is because the judgment is a prime number, so it does not enter the break point in if (i%j==0).

Keep pressing F10 (Step Over) to understand the operation of multiple loops step by step, allowing you to have a deeper understanding of loops.

 

During debugging, you can also change variables at any time. For example, when entering i=3 and j=1 in the figure below, the author clicked the i variable twice and modified the content to 6. After pressing F10, I found that the terminal screen had started from 6×1=6.

 

You can try changing any variable, and sometimes debugging will help you find the problem.

 

In addition to existing variable viewing, you can also customize calculation variables. As shown below, press the [+] icon button in the WATCH area to add the calculated values ​​you want to view.

 

For example, the author enters i*j to view the changes.

Press F10 to experience it yourself slowly. Instead of looking at the instructions on the screen, try it out yourself!

 

Further reading:

  • Python language that can be easily learned even without programming background
  • Visual Studio Code, a good partner for learning Python language
  • Basic introduction to using the Visual Studio Code interface (using Python)

Source: KOCPC Chinese

Tags: debugPython

Recent Posts

  • The Xiaomi Pad 8S Pro has passed network access certification and will debut with the self-developed XRING O3 chip.
  • The entire Google Pixel 11 lineup has been leaked! Official promotional renders of the Pixel 11 Pro XL have also surfaced
  • Are Chinese phone battery capacities falsely labeled? A brief look at the “capacity locking” phenomenon in Chinese silicon-carbon batteries.
  • NCC is leaderless, recklessly sending out national-level alert messages!?
  • What does “QR” in QR Code mean?

Recent Comments

No comments to show.
  • About Us

We welcome partnership inquiries and product review opportunities from smartphone manufacturers, iPhone accessory brands, and app developers.koc kocpc.com.tw|Privacy Policy |Hosting & Maintenance: Fast Line Taiwan, A-Chang Digital Technology

No Result
View All Result
  • Home
  • Tech News
  • AI News
  • Apps & Tutorials
  • Mobile & Telecom
  • Lifestyle
  • About Us

We welcome partnership inquiries and product review opportunities from smartphone manufacturers, iPhone accessory brands, and app developers.koc kocpc.com.tw|Privacy Policy |Hosting & Maintenance: Fast Line Taiwan, A-Chang Digital Technology