在vscode中设置PYTHONPATH

问题

搭建开发环境遇到了上述问题, 本来以为很简单, 实际上倒腾了2-3小时, 在此做个记录.

PYTHONPATH 是什么.

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

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, 没有跳转总觉得少了点什么.

步骤

1. set env file path.

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

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

2. use env file

.vscode/.env

PYTHONPATH=./your_src_dir:${PYTHONPATH}

done

最大的坑点是不能使用绝对路径, 这里我没有看代码, 不能用绝对路径绝对是BUG:

PYTHONPATH=${workspaceFolder}/your_src_dir:${PYTHONPATH}

你可能感兴趣的:(python)