在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}
相关文章
相关标签/搜索