首頁  >  文章  >  後端開發  >  vscode如何遠端偵錯python程式碼

vscode如何遠端偵錯python程式碼

WBOY
WBOY轉載
2023-04-30 21:13:051274瀏覽

環境設定

設定python 環境

準備一段python 程式碼

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)

然後在左側運行和調試按鈕中,點擊“創建launch.json”文件,選擇python文件(如果沒有的話需要先安裝python 擴展,在應用中搜索python 第一個安裝了最多的即可)

選擇python 檔案

產生預設的launch 檔案如下

{
    // 使用 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
        }
    ]
}

這裡我們需要自訂指定一下用到的python 版本,需要新增「pythonPath」選項

#
{
    // 使用 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
        }
    ]
}

這樣的話就可以使用指定的python 運行程式碼了

如果說用到了conda 虛擬環境,則需要找到虛擬環境對應的python 路徑,可以使用whereis python 查看

調試程式碼

配置好調試環境後,在程式碼中打上斷點,然後點選執行偵錯和執行按鈕,即可進入偵錯頁面

以上是vscode如何遠端偵錯python程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除