如何在 debug 下运行 Python 程序并设置运行的工作目录?
最佳答案
@SpeedCoder5's comment 值得回答。
在 launch.json
中,指定一个动态工作目录(即当前打开的 Python 文件所在的目录):
"cwd": "${fileDirname}"
这利用了 "variables reference" feature in VS Code 和预定义变量 fileDirname
。
如果您在运行 Python 时使用 Python: Current File (Integrated Terminal)
选项,您的 launch.json
文件可能与我的类似,如下所示 (more info on launch.json
files here )。
{
"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 ...
}
Remember the launch.json
file controls the run/debug settings of your Visual Studio code project ;我的 launch.json
文件是由 VS Code 在我当前的“打开项目”目录中自动生成的。我只是手动编辑文件以添加 "cwd": "${fileDirname}"
如上所示。
记住 launch.json
文件可能特定于您的项目或特定于您的目录,因此请确认您正在编辑 正确 launch.json
(见评论)
如果你没有 launch.json
文件,try this:
To create a launch.json file, open your project folder in VS Code (File > Open Folder) and then select the Configure gear icon on the Debug view top bar.
关于python - VSCode -- 如何设置调试 Python 程序的工作目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38623138/