[英]VSCode -- how to set working directory for debugging a Python program
如何在調試下運行 Python 程序並設置運行的工作目錄?
@SpeedCoder5 的評論值得回答。
在launch.json
中,使用以下命令指定一個動態工作目錄(即當前打開的 Python 文件所在的目錄):
"cwd": "${fileDirname}"
這利用了VS Code 中的“變量引用”功能和預定義的變量fileDirname
。
如果您在運行 Python 時使用Python: Current File (Integrated Terminal)
選項,那么您的launch.json
文件可能看起來像我的,如下所示( 有關launch.json
文件的更多信息,請點擊此處)。
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File (Integrated Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${fileDirname}"
},
//... other settings, but I modified the "Current File" setting above ...
}
請記住, launch.json
文件控制 Visual Studio 代碼項目的運行/調試設置; 我的launch.json
文件是由 VS Code 在我當前的“打開項目”目錄中自動生成的。 我只是手動編輯了文件以添加"cwd": "${fileDirname}"
如上所示。
請記住, launch.json
文件可能特定於您的項目或特定於您的目錄,因此請確認您正在編輯正確的launch.json
(請參閱評論)
如果您沒有launch.json
文件,請嘗試以下操作:
要創建 launch.json 文件,請在 VS Code 中打開項目文件夾(文件 > 打開文件夾),然后選擇調試視圖頂部欄上的配置齒輪圖標。
在launch.json
中配置cwd
設置如下:
{
"name": "Python",
"type": "python",
"pythonPath": "python",
...
"cwd": "<Path to the directory>"
...
}
此設置對我有幫助:
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"cwd": "${workspaceFolder}\\app\\js", // set directory here
"program": "${workspaceFolder}\\app\\js\\server.js", // set start js here
}
在某些情況下,將PYTHONPATH
與workspaceFolder
一起設置也可能很有用:
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${workspaceFolder}",
"env": {
"PYTHONPATH": "${cwd}"
}
}
我正在為在 Node.js 上使用 TypeScript 的人發布此示例配置
在我的項目中,我的 Node.js 服務器 TypeScript 文件位於文件夾 Application_ts 中,編譯后的 js 文件在名為 Application 的文件夾中生成
因為當我們在調試模式下運行我們的應用程序或正常啟動它時,我們應該從包含 js 文件的 Application 文件夾開始,所以下面的配置從我的 application_ts 也存在的根文件夾運行調試並且工作完美
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug TypeScript in Node.js",
"program": "${workspaceRoot}\\Application\\app.js",
"cwd": "${workspaceRoot}\\Application",
"protocol": "inspector",
"outFiles": [],
"sourceMaps": true
},
{
"type": "node",
"request": "attach",
"name": "Attach to Process",
"port": 5858,
"outFiles": [],
"sourceMaps": true
}
]
}
您可以使用launch.json
中的cwd
參數為調試程序設置當前工作目錄
要將當前工作目錄設置為您當時正在執行的任何文件:
文件 > 首選項 > 設置 > Python > 數據科學 > 在文件目錄中執行
謝謝brch:VSCode 中的Python:每次都將工作目錄設置為python文件的路徑
我遇到了同樣的問題,並注意到當在 Mac 的終端中運行which python
命令時,它顯示了與我在 vs 代碼中運行which python
命令時得到的路徑不同的路徑。 而且當使用python filename.py
運行時,我的文件在終端中運行正常
因此,我從終端復制了該路徑並將其粘貼到 VS 代碼中的 Preferences->Settings->Extensions->Python->Default Interpreter Path 中,它起作用了。 我希望這有幫助。
我使用“justMyCode = false”所以我也可以調試並跳轉到主腳本調用的函數。
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": false,
"cwd": "${fileDirname}" }
]
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.