在 VSCode 中幫需要命令行引數的 Python 程式除錯

codemee - Sep 18 - - Dev Community

在 VSCode 中如果要除錯的 Python 程式需要透過命令行傳入引數, 就必須透過 launch.json 檔除錯, 這可以按∨(執行或偵測...) 鈕候選 Python 真錯工具:使用 launch.json 進行偵錯

操作圖

接著選 Python Debugger

操作圖

再選具有引數的 Python 檔案, 就會自動幫你建立需要輸入引數的 launch.json 檔:

操作圖

建立檔案後它就會啟動偵錯程序, 首先就會要你輸入命令行的引數:

操作圖

如果你查看檔案總管, 就會看到自動建立了 .vscode 資料夾, 並且在其中新增了 launch.json 檔:

操作圖

這個檔案的內容如下:

{
    // 使用 IntelliSense 以得知可用的屬性。
    // 暫留以檢視現有屬性的描述。
    // 如需詳細資訊,請瀏覽: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python 偵錯工具: 帶引數的目前檔案",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "args": [
                "${command:pickArgs}"
            ]
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

args 設定為 "${command:pickArgs}" 就會帶出剛剛看到的輸入框讓你輸入引數, 不過這會把輸入的內容當成單一個引數, 你也可以自行填入引數, 例如:

{
...
            "args": [
                "1", "2", "3"
            ]
...
}
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player