如何在 VS Code 中为 pylint 设置工作目录?

How to setup working directory in VS Code for pylint?

我有一个具有该结构的项目:

├── .git
├── .gitignore
├── README.md
├── requirements.txt
└── src

Pylint 默认情况下 运行 来自项目根目录,我的所有导入都有错误,因为源根目录在 src 目录中。 我尝试在 settings.json 中设置 linter 路径,但是 linter 不起作用

"python.linting.pylintPath": "cd src && pylint"

问题是:如何在 VS Code 中更改 pylint 的源代码根目录?我使用这个扩展 https://github.com/DonJayamanne/pythonVSCode

您可以通过在项目根目录中创建一个 .env 文件来解决此问题,内容为:

PYTHONPATH=./src

将此行添加到您的 settings.json 文件中(在 .vscode 目录中)。

"python.autoComplete.extraPaths": ["./src"],

PYTHONPATHpython 的路径,而不是工作目录。

更好的方法是自定义Settings.jsonlaunch.json,像这样:

// vi .vscode/Settings.json
{
    "python.pythonPath": "venv/bin/python",
    "python.linting.pylintEnabled": true,
    "python.linting.enabled": true,
    "python.formatting.provider": "autopep8"
}

// vi .vscode/launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: your project name",
            "type": "python",
            "request": "launch",
            "cwd": "${workspaceRoot}/src",
        }
    ]
}

参考:https://code.visualstudio.com/docs/editor/debugging#_launch-versus-attach-configurations