如何在调试下运行Python程序并设置运行的工作目录?
当前回答
设置当前工作目录为当前正在执行的文件:
File > Preferences > Settings > Python > Data Science >在File Dir执行
brch: Python in VSCode:每次设置工作目录为Python文件的路径
其他回答
你可以在launch.json中使用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
}
在某些情况下,将PYTHONPATH与workspaceFolder一起设置可能也很有用:
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${workspaceFolder}",
"env": {
"PYTHONPATH": "${cwd}"
}
}
我也遇到了同样的问题,并注意到当在Mac的终端中运行which python命令时,它会显示与我在vs code中运行which python命令时所得到的路径不同。并且当使用python filename.py运行时,我的文件在终端中正常运行
所以我从终端复制了该路径,并将其粘贴到VS代码中的首选项->设置->扩展->Python->默认解释器路径,它工作了。我希望这能有所帮助。
我使用“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}" }
]
}