如何在调试下运行Python程序并设置运行的工作目录?
当前回答
这个设置帮助我:(我是一个Windows用户)
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"cwd": "${workspaceFolder}\\app\\js", // set directory here
"program": "${workspaceFolder}\\app\\js\\server.js", // set start js here
}
其他回答
在启动时配置cwd设置。Json格式如下:
{
"name": "Python",
"type": "python",
"pythonPath": "python",
...
"cwd": "<Path to the directory>"
...
}
我也遇到了同样的问题,并注意到当在Mac的终端中运行which python命令时,它会显示与我在vs code中运行which python命令时所得到的路径不同。并且当使用python filename.py运行时,我的文件在终端中正常运行
所以我从终端复制了该路径,并将其粘贴到VS代码中的首选项->设置->扩展->Python->默认解释器路径,它工作了。我希望这能有所帮助。
在某些情况下,将PYTHONPATH与workspaceFolder一起设置可能也很有用:
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${workspaceFolder}",
"env": {
"PYTHONPATH": "${cwd}"
}
}
这个设置帮助我:(我是一个Windows用户)
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"cwd": "${workspaceFolder}\\app\\js", // set directory here
"program": "${workspaceFolder}\\app\\js\\server.js", // set start js here
}
我使用“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}" }
]
}