VSCODE設置當前項目目錄爲啓動目錄,解決無法import項目的其他pthon文件的問題

新建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"
        }
    ]
}

如果不知道這個文件怎麼創建,只要先運行項目,即使報錯也沒關係,他會提示你創建這個文件的

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章