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条回答
Rolldiameter
3楼-- · 2020-01-27 04:13

Accepted answer won't fix error when importing own modules. Use following setting in your workspace settings .vscode/settings.json

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

ref: https://github.com/microsoft/python-language-server/blob/master/TROUBLESHOOTING.md#unresolved-import-warnings

查看更多
Rolldiameter
4楼-- · 2020-01-27 04:15

Alternative way: use the command interface!

cmd/ctrl + shift + p > Python: Select Interpreter > choose the one with the packages you look for

enter image description here

查看更多
仙女界的扛把子
5楼-- · 2020-01-27 04:15

I have a different solution: my VSCode instance had picked up the virtualenv stored in .venv, but was using the wrong Python binary. It was using .venv/bin/python3.7; using the switcher in the blue status bar, I changed it to use .venv/bin/python and all of my imports were resolved correctly. I don't know what VSCode is doing behind the scenes when I do this, nor do I understand why this was causing my problem, but for me this was a slightly simpler solution than editing my workspace settings. I hope it helps someone.

查看更多
Explosion°爆炸
6楼-- · 2020-01-27 04:15

In my case I already had a conda environment activated but still wanted local python modules to be available for autocomplete, peeking definition, etc. I tried many solutions such as adding list of python paths etc.. but what finally solved it for me was to create a symlink from conda lib/python{your version}/site-packages to my local module.

查看更多
爷的心禁止访问
7楼-- · 2020-01-27 04:17

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

{    
    "python.jediEnabled": false
}
查看更多
登录 后发表回答