新建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"
}
]
}
如果不知道這個文件怎麼創建,只要先運行項目,即使報錯也沒關係,他會提示你創建這個文件的