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:17

None of the above worked for me. Adding both of the lines below to my settings.json file did, however.

"python.analysis.disabled": [ 
    "unresolved-import"
],
"python.linting.pylintArgs": ["--load-plugin","pylint_protobuf"] 

The first line really just hides the linting error. Certainly not a permanent solution, but de-clutters the screen.

This answer gave me the second line: VS Code PyLint Error E0602 (undefined variable) with ProtoBuf compiled Python Structure

Maybe someone who understands python more than me can explain that one more.

查看更多
▲ chillily
3楼-- · 2020-01-27 04:19

My solution. This solution is only for the current project.

  1. In the project root create folder .vscode
  2. Then create the file .vscode/settings.json
  3. in the file setting.json add the line (this is for python3)
{
    "python.pythonPath": "/usr/local/bin/python3",
}
  1. This is the example for python 2
{
    "python.pythonPath": "/usr/local/bin/python",
}
  1. If you don't know where is located your python just run on the terminal the command which python or which python3 in will print the python location.

  2. This example works for dockerized Python - Django.

查看更多
Evening l夕情丶
4楼-- · 2020-01-27 04:24

The solution from @Shinebayar G worked but this other one is a little bit more elegant:

Copied from https://github.com/Microsoft/vscode-python/issues/3840#issuecomment-463789294:

Given the following example project structure:

  • workspaceRootFolder
    • .vscode
    • ... other folders
    • codeFolder

What I did to resolve this issue:

  1. Go into the workspace folder (here workspaceRootFolder) and create a .env file
  2. In this empty .env file add the line PYTHONPATH=codeFolder (replace codeFolder with your folder name)
  3. Add "python.envFile": "${workspaceFolder}/.env" to the settings.json
  4. Restart VS code
查看更多
forever°为你锁心
5楼-- · 2020-01-27 04:24

I have resolved import error by CTRL+Shift+P type Preferences settings and select the option Preferences Open Settings (JSON) and add a line "python.pythonPath": "/usr/bin/" So JSON should look like

{
    "python.pythonPath": "/usr/bin/"
}

Keep other configuration lines if it is present. This should import all modules that you have installed using PIP for autocomplete.

查看更多
疯言疯语
6楼-- · 2020-01-27 04:27

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

{
    "python.pythonPath": "/path/to/your/venv/bin/python",
}
查看更多
我欲成王,谁敢阻挡
7楼-- · 2020-01-27 04:27

Incase of pylint error install the following

pipenv install pylint-django

Then create a file .pylintrc in the root folder and write the following

load-plugins=pylint-django
查看更多
登录 后发表回答