How to setup working directory in VS Code for pylint?

Question:

I have a project with that structure:

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

Pylint by default running from project root and I have error on all my imports, because source root in src directory.
I try to setup the linter path in settings.json, but then linter don’t work

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

Question is: how to change the source root for pylint in VS Code? I use this extension https://github.com/DonJayamanne/pythonVSCode

Asked By: Arr

||

Answers:

You can solve this by creating a .env file in the project root with content:

PYTHONPATH=./src
Answered By: Arr

Add this line in your settings.json file (in the .vscode directory).

"python.autoComplete.extraPaths": ["./src"],
Answered By: Marco Lavagnino

The PYTHONPATH is the path to python, not the working directory .

The better way is to customize Settings.json and launch.json, do like this:

// 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",
        }
    ]
}

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

Answered By: NicoNing

In vs-code:
File – Preferences – Settings
Search for: Pylint
"Pylint: Path" – Add item

It work for me!

Answered By: dipbox

With the newer separate Pylint VSCode extension, the only thing that worked for me was adding "pylint.args": ["--rcfile=PATH_TO/pylintrc"] to my VSCode settings.json.

Answered By: Akh