187
votes

I am using the following setup

  • macOS v10.14 (Mojave)
  • Python 3.7.1
  • Visual Studio Code 1.30
  • Pylint 2.2.2
  • Django 2.1.4

I want to use linting to make my life a bit easier in Visual Studio Code. However, for every import I have states "unresolved import". Even on default Django imports (i.e. from django.db import models).

I presume it is because it is not seeing the virtual environment Python files.

Everything works just fine, but it's starting to get annoying.

The interpreter choices I have are all system versions of Python. It does not seem to see my virtual environment Python at all (it is not in the same directory as my workspace, so that part makes sense).

If I set up the python.PythonPath in the settings.json file, it just ignores it and does not list my virtual environment path as an option. I also tried setting it up in my global Python settings, but it also does not show up.

Is there a quick fix to get it working?

11
pip install pylint-django, then in vs code settings add this: "python.linting.pylintArgs": [ "--load-plugins=pylint_django", ], - Vaibhav Vishal
@VaibhavVishal unfortunately, i still get the same results (added it to my settings.json file) - jAC
Its actually a VScode problem that detects import package very late , after restarting it is fixed automatically. - Anupam Haldkar
@VaibhavVishal this is the only solution that worked for me in the entire thread. Thanks. - David D.
My pythonPath was already set correctly to the venv python. If this helps anyone, I actually had to install pylint in my venv: python -m pip install pylint, and then update my pylintPath in VS Code to the venv pylint. - Tim

11 Answers

143
votes

In your workspace settings, you can set your Python path like this:

{
    "python.pythonPath": "/path/to/your/venv/bin/python",
}
280
votes

The accepted answer won't fix the error when importing own modules.

Use the following setting in your workspace settings .vscode/settings.json:

"python.autoComplete.extraPaths": ["./path-to-your-code"],

Reference: Troubleshooting, Unresolved import warnings

80
votes

Alternative way: use the command interface!

Cmd/Ctrl + Shift + PPython: Select Interpreter → choose the one with the packages you look for:

Enter image description here

51
votes

This issue has already been opened on GitHub:

Python unresolved import issue #3840

There are two very useful answers, by MagnuesBrzenk and SpenHouet.

The best solution for now is to create a .env file in your project root folder. Then add a PYTHONPATH to it like this:

PYTHONPATH=YOUR/MODULES/PATH

And in your settings.json add:

"python.envFile": ".env"
33
votes
23
votes

If you have this code in your settings.json file, delete it:

{
    "python.jediEnabled": false
}
20
votes

If you are more visual like myself, you can use the Visual Studio Code configurations in menu FilePreferencesSettings (Ctrl + ,). Go to ExtensionsPython.

In the section Analysis: Disabled, add the suppression of the following message: unresolved-import:

Visual Studio Code settings

20
votes

None of the solutions worked except this one. Replacing "Pylance" or "Microsoft" in the settings.json solved mine.

"python.languageServer": "Jedi"
19
votes

I was able to resolved this by enabling jedi in .vscode\settings.json

"python.jediEnabled": true

Reference from https://github.com/Microsoft/vscode-python/issues/3840#issuecomment-456017675

15
votes

You need to select the interpreter associated with the virtual environment.

Enter image description here

Click here (at the bottom status bar):

Enter image description here

And just select the virtual environment you are working with. Done.

Sometimes, even with the interpreter selected, it won't work. Just repeat the process again and it should solve it.

Enter image description here

14
votes

I wonder how many solutions this problem have (or have not), I tried most of the above, nothing worked, the only solution that worked is to set the python language server to Jedi, instead of Microsoft in the settings.json file:

"python.languageServer": "Jedi"