在VScode中执行python脚本或者debug的时候,加载文件路径为相对路径时,提时异常.主要是Vs code运行时启动的Python解释器是在项目文件夹主路径,如果代码在主项目子文件夹,使用相对路径,可能会提示报错等信息。
解决解决方案:
1. 点击右上角[文件]->[首选项]->[设置]->[用户]->[扩展]
在[设置]页,搜索.terminal.executeInFileDir,点击选中
2. 点击右上角[运行]->[添加配置],打开launch.json文件中添加 "cwd": "${fileDirname}",如下:
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: 当前文件",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
// 添加这一行
"cwd": "${fileDirname}"
}
]
}
然后,重启VScode即可!!!