Home  >  Article  >  Development Tools  >  How to remotely debug python code with vscode? Method arrangement

How to remotely debug python code with vscode? Method arrangement

青灯夜游
青灯夜游forward
2023-04-12 18:00:173445browse

In the article "vscode remote gdb debugging", we introduce how to use vscode to debug c/c code. As a companion article to this article, this article summarizes the methods of debugging python code.

How to remotely debug python code with vscode? Method arrangement

Environment configuration

The remote connection method is the same as No. 1 in "vscode remote gdb debugging" The sections are the same and will not be repeated in this article. Those who are not familiar with them can refer to that article.

Configure the python environment

Prepare a piece of python code

from __future__ import print_function

def sum_nums(n):
    s=0
    for i in range(n):
        s += i
        print(s)
 
if __name__ == '__main__':
    sum_nums(5)

Then in the run and debug buttons on the left, click "Create launch.json" file , select the python file (if not, you need to install the python extension first, search for python in the application and install the most first one) [Recommended learning: vscode tutorial, Programming Tutorial

Select the python file

Generate the default launch file as follows

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: 当前文件",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true
        }
    ]
}

Here we need To customize the python version used, you need to add the "pythonPath" option

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: 当前文件",
            "type": "python",
            "pythonPath": "/home/lthpc/anaconda3/bin/python3.7",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true
        }
    ]
}

In this way, you can use the specified python to run the code

If the conda virtual environment is used, you need To find the python path corresponding to the virtual environment, you can use whereis python to view, such as

Debugging code

After configuring the debugging environment, Put a breakpoint in the code, and then click the run debugging and execution buttons to enter the debugging page

The rest of the operations are very familiar and will not be repeated

For more knowledge about VSCode, please visit: vscode Basic Tutorial!

The above is the detailed content of How to remotely debug python code with vscode? Method arrangement. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete