duckAsylum
duckAsylum

Reputation: 41

Visual Studio Code Python linting not working with venv and wsl

I am using VSCode in Windows 10 as my Python IDE. I use wsl.exe as my terminal inside VSCode. For every project I create a separate venv (in wsl.exe) and install all python module/package dependencies with pip. To run the python code (inside the venv) and to get into and out of the venv I use wsl.exe terminal.

In my venvs I have python 3.6.7 which comes from my Ubuntu WSL. The VSCode python interpreter is 3.7.1. The problem is that the pylint in VSCode can't find the imports although the programs run OK. Pylint is installed using pip in every venv.

There have to be some settings to tell pylint to look for the imports from the venv and not from the native VSCode python interpreter destination.

I tried with setting "python.venvPath": "C:\\User\\username\\Desktop\\Python\\venvname" but no luck. Could it be a problem that my venvs are using a linux pyhton and my VSCode is using a windows version?

I also tried creating a work-space specific pylint file and added #init-hook='import sys; sys.path.append("C:\Users\username\Desktop\Python\venv\lib\python3.6")' although it removed the errors I broke linting as now I could write gibberish imports.

Although it does not make VSCode unusable the red lines under import get really annoying.

Thank you all in advance.

Upvotes: 4

Views: 6024

Answers (3)

JeffreyShran
JeffreyShran

Reputation: 117

2023 Update

The python path is slightly different now, try this in 2023.

"python.defaultInterpreterPath": "~/[venv-path-here]/bin/python",

Upvotes: 1

razor_chk
razor_chk

Reputation: 131

Make sure you have installed a linter (example pylint) in your venv, typically by running pip install pylint (whiles your venv is activated)

Your workspace should contain a .vscode folder if you have already set your virtual environment. If not, create one...

In your .vscode folder, open (or create) the settings.json file:

The file should contain (or you should add):

{
 ...

    "python.pythonPath": "/path-to-venv's-top-folder/bin/python3",

    "python.linting.pylintPath": "/path-to-venv's-top-folder/bin/pylint",

    "python.linting.pylintEnabled": true,
    "python.linting.enabled": true
}

[This is in case you're using pylint]

Upvotes: 1

Youssef
Youssef

Reputation: 1495

One possibility is to modify your settings.py (workspace-settings of your current project) by adding:

"python.pythonPath": "C:\\path\\to\\venvpath\\your-venv\\python"

You'll also have to add

"python.linting.pylintPath": "C:\\path\\to\\venvpath\\your-venv\\pylint"

to your workspace-settings as well. It's probably necessary to append .exe to the executables (not sure, because I'm on Linux).

If that doesn't work, I'd recommend to switch to Python 3.6.7, which you used to create the virtual envs. But 3.7.1 should work though.

Upvotes: 0

Related Questions