我可以看到這個問題已經被問過很多次了,但是似乎沒有一個解決方案再起作用,迫使我要求最新的修復程式。
問題
我在 VS 代碼中創建了一個具有以下目錄結構的作業區,

- work__assignments(這是我在 vs 代碼中的主要作業區檔案夾)
- 作業__1
- 資料
- 模塊
- 測驗
- 作業__2
- 資料
- 模塊
- 測驗
- 作業__1
“work__assignments”是我在 VS 代碼中的主要作業區檔案夾。從這個主作業區,我轉到一個分配檔案夾(例如work__assignments > assignment__2 > modules)并處理相應的代碼。但是,當我嘗試在“ work__assignments > assignment__2 > modules ”中除錯代碼時,除錯器會從主作業區檔案夾(即 work__assignments)加載,然后它會失敗,因為它在模塊檔案夾“ work__assignments > assignment__2 >模塊”。
到目前為止,我已經嘗試了以下方法,
- 更新 launch.json 檔案并添加行
"cwd": "${fileDirname}",
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${fileDirname}"
}
- 更新 launch.json 檔案并添加行
"cwd": "",
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": ""
}
- 更新 launch.json 檔案并添加以下行,
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${workspaceFolder}",
"env": {
"PYTHONPATH": "${cwd}"
}
}
更新首選項

然而,這些方法都沒有奏效。每次我除錯代碼時,它都會啟動到作業區檔案夾而不是代碼檔案夾中。我究竟做錯了什么?
注意:我正在使用右上角的“除錯 Python 檔案”按鈕運行除錯器,如下圖所示,

uj5u.com熱心網友回復:
cwd在launch.json中配置以下內容之一:
"cwd": "./assignment__2"
"cwd": "${fileDirname}"
需要注意的是,您需要從Run and Debug面板除錯程式才能執行launch.json中的配置。

一個簡單的例子
目錄結構和代碼

啟動.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
// "cwd": "./hello"
"cwd": "${fileDirname}"
}
]
}
除錯結果

launch.json如果從播放按鈕中選擇除錯,則配置無效

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/510551.html
