新建launch.json文件,加入以下指令即可
"env": {"PYTHONPATH":"${workspaceFolder}"}, // 添加此项才能解决vscode的python运行目录的问题,否则会无法正常引用当前项目目录中的其他文件
"envFile": "${workspaceFolder}/.env"
{
// 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: 当前文件",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"pythonPath": "${config:python.pythonPath}",
"cwd": "${workspaceFolder}",
"env": {"PYTHONPATH":"${workspaceFolder}"}, // 添加此项才能解决vscode的python运行目录的问题,否则会无法正常引用当前项目目录中的其他文件
"envFile": "${workspaceFolder}/.env"
}
]
}
如果不知道这个文件怎么创建,只要先运行项目,即使报错也没关系,他会提示你创建这个文件的