How to setup working directory in VS Code for pylint?

Viewed 10019

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

4 Answers

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

PYTHONPATH=./src

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

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

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

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

It work for me!

Related