Pylint “unresolved import” error in visual studio

2020-01-27 03:44发布

I am using the following setup

  • MacOS 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, 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 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 setup 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.

Has anyone run into this issue and know a quick fix to get it working?

Thanks, jAC

16条回答
闹够了就滚
2楼-- · 2020-01-27 04:27

my solution was to open vscode in a previous directory.

查看更多
成全新的幸福
3楼-- · 2020-01-27 04:29

This works for me:

Open the command palette (Ctrl-Shift-P) and choose "Python: Select Interpreter". Doing this you set the Python interpreter in VSCode

查看更多
Animai°情兽
4楼-- · 2020-01-27 04:30

This issue has already been opened on GitHub: https://github.com/Microsoft/vscode-python/issues/3840 There are 2 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"  
查看更多
仙女界的扛把子
5楼-- · 2020-01-27 04:32

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

查看更多
登录 后发表回答