14

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

Gama11
  • 31,714
  • 9
  • 78
  • 100
Arr
  • 749
  • 3
  • 6
  • 15

5 Answers5

9

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

PYTHONPATH=./src
mjr104
  • 193
  • 1
  • 4
Arr
  • 749
  • 3
  • 6
  • 15
  • 4
    This clearly doesn't work *unless* you `export PYTHONPATH=./src` in the terminal. – gented Sep 12 '18 at 14:10
  • @gented not even when you set pyenv path in workspace settings? – jave.web Feb 25 '20 at 20:14
  • 2
    With the current VS Code Python extension, you *do not* need to run `export PYTHONPATH=./src` if you have a `.env` file with the above contents in your workspace folder. The extension will automatically use any environment variables set in the `.env` file. [See the docs for more info.](https://code.visualstudio.com/docs/python/environments#_environment-variable-definitions-file) – Cameron Bieganek Sep 30 '20 at 18:25
  • This works for me but not when I run a file in VScode. Any hints there? – Roelant Nov 24 '20 at 12:26
8

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

"python.autoComplete.extraPaths": ["./src"],
Marco Lavagnino
  • 1,140
  • 12
  • 31
  • In case you want to find this through GUI => go to Settings => Extensions => Python => "Auto Complete: Extra Paths" => Edit in settings.json (hopefuly in future this will change to GUI dynamic list of input fields :) ) – jave.web Feb 25 '20 at 20:13
0

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

NicoNing
  • 3,076
  • 12
  • 23
  • 2
    No, `PYTHONPATH` is the list of directories where Python looks for modules. [See the docs.](https://docs.python.org/3/using/cmdline.html#envvar-PYTHONPATH) – Cameron Bieganek Sep 30 '20 at 18:27
0

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.

Akh
  • 632
  • 1
  • 6
  • 15
-1

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

It work for me!

dipbox
  • 1