@SpeedCoder5的评论应该是一个答案
具体来说,可以使用"cwd": "${fileDirname}"指定动态工作目录(即当前打开的Python文件所在的目录)
如果在运行Python时使用Python: Current File (Integrated Terminal)选项,那么您的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文件,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.