在vscode中設置PYTHONPATH

問題

搭建開發環境遇到了上述問題, 原本覺得很簡單, 實際上倒騰了2-3小時, 在此作個記錄.python

PYTHONPATH 是什麼.

https://code.visualstudio.com...json

In VS Code, PYTHONPATH affects debugging, linting, IntelliSense, unit testing, and any other operation that depends on Python resolving modules. For example, suppose you have source code in a src folder and tests in a tests folder. When running tests, however, they can't normally access modules in src unless you hard-code relative paths. To solve this problem, add the path to src to PYTHONPATH.

能夠看到PYTHONPATH會影響不少東西. 對開發影響最大的就是linting, IntelliSense, unit testing了. 我習慣了現代化IDE, 沒有跳轉總以爲少了點什麼.less

步驟

1. set env file path.

https://code.visualstudio.com...
默認是${workspaceFolder}/.env
我修改.vscode/settings.json爲了${workspaceFolder}/.vscode/.env:this

{
    ...
        "python.envFile": "${workspaceFolder}/.vscode/.env",
    ...
}

2. use env file

.vscode/.envspa

PYTHONPATH=./your_src_dir:${PYTHONPATH}

donedebug

最大的坑點是不能使用絕對路徑, 這裏我沒有看代碼, 不能用絕對路徑絕對是BUG:code

PYTHONPATH=${workspaceFolder}/your_src_dir:${PYTHONPATH}
相關文章
相關標籤/搜索