Pylint "unresolved import" error in Visual Studio Code

348,196

Solution 1

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

{
    "python.defaultInterpreterPath": "/path/to/your/venv/bin/python",
}

Solution 2

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

Solution 3

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

Solution 4

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"

Solution 5

When I do > reload window that fixes it.

Reference: Python unresolved import issue #3840, dkavraal's comment

Share:
348,196

Related videos on Youtube

jAC
Author by

jAC

During my day life, I am a technical manager make sure that our support staff has the resources they need to their jobs. I conduct application testing and providing assistance where ever possible. My side business is web development mainly. I started out a long time ago with PHP and now have moved on to Python/Django to fill those needs. I am a self taught programer and as my posts here show that, I still like to learn as much as i can while making these mistakes to learn from. Music is my other life. I am a drummer and like to learn new musical things as much as i do in programming.

Updated on July 27, 2022

Comments

  • jAC
    jAC almost 2 years

    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?

    • Vaibhav Vishal
      Vaibhav Vishal over 5 years
      pip install pylint-django, then in vs code settings add this: "python.linting.pylintArgs": [ "--load-plugins=pylint_django", ],
    • jAC
      jAC over 5 years
      @VaibhavVishal unfortunately, i still get the same results (added it to my settings.json file)
    • Anupam Haldkar
      Anupam Haldkar almost 4 years
      Its actually a VScode problem that detects import package very late , after restarting it is fixed automatically.
    • David Dahan
      David Dahan over 3 years
      @VaibhavVishal this is the only solution that worked for me in the entire thread. Thanks.
    • Tim
      Tim about 3 years
      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.
  • jAC
    jAC over 5 years
    This sort of works. For the Python specific imports it resolves those now but not my own models (i.e. "from users.models import User" still says it cannot resolve it). Thoughts on that?
  • ruddra
    ruddra over 5 years
    Not sure. Please try reloading the window of the vs code ( from shell, code <project_directory> -r) or just restart the vscode.
  • jAC
    jAC over 5 years
    Yeah tried all that, still doesn't recognize them for some reason. Not sure what is going on with it.
  • ruddra
    ruddra over 5 years
    I am not sure, sometimes it may occur if the workspace directory is not set properly. Please make sure manage.py is in root of workspace. Also please make sure the pylint is configured properly
  • jAC
    jAC over 5 years
    you genius! It was because my workspace was not starting at the root level of the project. Once i adjusted that it started work. Thanks so much!
  • Tiw
    Tiw about 5 years
    It's better to add a bit of explanation.
  • KowaiiNeko
    KowaiiNeko about 5 years
    Thanks, that worked perfectly! "${workspaceFolder}/.env" PYTHONPATH=FolderName
  • Tomasz Chudzik
    Tomasz Chudzik about 5 years
    Editing this line is a big change in your project. Line: "python.jediEnabled": false disables the old language server and enables the new Microsoft Python Language Server. Look here: github.com/Microsoft/vscode-python/issues/2177 I think it's much simpler to just add necessary dependencies to our envFile. It's described in another answer. With the new Microsoft Python Language Server intellisense works much better for me.
  • jAC
    jAC almost 5 years
    in my case this did not work but hopefully it helps others with this issue.
  • ted
    ted almost 5 years
    yeah that's the spirit, it's not a complicated solution, it's what happened for me :)
  • Tomasz Chudzik
    Tomasz Chudzik almost 5 years
    There was a common issue with editable installs when using the Microsoft Python Language Server. However it looks like currently after the new fix described here: github.com/microsoft/python-language-server/issues/… the issue has been fixed and any import can be added with ExtraPaths. Look at this TroubleShooting for more help: github.com/microsoft/python-language-server/blob/master/…
  • fynmnx
    fynmnx almost 5 years
    This worked for me. Incase anyone was still wondering in 2019.
  • Admin
    Admin almost 5 years
    Thanks so much! Helped me get past an ages-old problem!
  • nairb
    nairb almost 5 years
    For me its the other way around, enabling jedi in settings.json works for me. See more here, stackoverflow.com/a/57269144/2877493
  • Robert
    Robert almost 5 years
    Thanks @TomaszChudzik - Setting: "python.autoComplete.extraPaths": ["./src"] worked like a charm!
  • dillon.harless
    dillon.harless over 4 years
    @Safder not for me. using virtualenv
  • dillon.harless
    dillon.harless over 4 years
    @jAC can you elaborate a little more? I can't seem to figure this out.
  • dillon.harless
    dillon.harless over 4 years
    This didn't work for me. If it helps anyone, adding these two lines to user settings did: "python.analysis.disabled": [ "unresolved-import" ], "python.linting.pylintArgs": ["--load-plugin","pylint_protobuf"]
  • Epitheoritis 32
    Epitheoritis 32 over 4 years
    Another solution is to add your codebase modules in your virtualenv (using add2virtualenv YOUR/MODULES/PATH for example), and select this virtualenv as your python interpreter.
  • vijay athithya
    vijay athithya over 4 years
    It just hides the warning if I click on the file, again it shows up!
  • daisy
    daisy over 4 years
    I have a mixed workspace, all python code is in a sub folder. Adding a .vscode in the sub folder will not work, this is the only working solution.
  • None
    None over 4 years
    best answer for vscode settings.json. (if using workspace.xml the accepted answer might be the way to go, but can't say for sure. i'm also using a mixed workspace) many thanks for solution.
  • greendino
    greendino about 4 years
    I have been enabled since the beginning. lucky you
  • greendino
    greendino about 4 years
    in my case. my python 2 interpreter works fine. just my python 3 not able to cooperate very well
  • Richard Li
    Richard Li about 4 years
    This should be the answer.
  • Richard Li
    Richard Li about 4 years
    The answer with "python.autoComplete.extraPaths" solved my problem with the GitHub link that gives the reason.
  • Avid Programmer
    Avid Programmer about 4 years
    This worked for me! The reference link explains perfectly. TL;DR; For user created local scripts in subdirectories, the above setting helps the python interpreter to recognize the subdirectory as its own workspace. Subsequently, recognizing scripts in the workspace and resolving imports when whole modules or individual methods are imported.
  • Jonathan
    Jonathan about 4 years
    Reloading window worked for resolving the issue for my own models.
  • Satya Mishra
    Satya Mishra almost 4 years
    The unresolved-import for relative imports is clearly incorrect, this gets rid of it.
  • alan23273850
    alan23273850 almost 4 years
    Hello everyone! I've found that ["./path-to-your-code"] can be ["./**"] in any case where a double star means any sub-folder under the root directory! This is a simpler solution.
  • James DeRagon
    James DeRagon almost 4 years
    This worked for me on a mac as well, but I had to put the absolute path from root all the way up to the directory with the files being added in order to import any of the other python files in the directory.
  • Peter Mortensen
    Peter Mortensen almost 4 years
    Is ">" literal or part of a prompt?
  • Peter Mortensen
    Peter Mortensen almost 4 years
    What do you mean by "previous directory"? Do you mean "parent directory"? Or something else?
  • Peter Mortensen
    Peter Mortensen almost 4 years
    What do you mean by "check Pylint"? A check box? That it exists/is installed? Something else? Respond by editing your answer, not here in comments (and without "Edit:", "Update:", or similiar).
  • Peter Mortensen
    Peter Mortensen almost 4 years
    What is "code-runner"? Can you add a reference to it (by editing your answer, not here in comments)?
  • Jeremias Caceres
    Jeremias Caceres almost 4 years
    I only open VSCode with the console in a directory before the project
  • Mahm00d
    Mahm00d almost 4 years
    This was exactly my problem (on mac) and selecting the proper interpreter solved it. Thanks!
  • retrovius
    retrovius almost 4 years
    This worked for me! The relative path works on windows. But this is a sloppy workaround and shouldn't have to be done. I wish the authors could fix this issue
  • Adrian Gonzalez
    Adrian Gonzalez almost 4 years
    Don't forget to reload your vs code window after adding this for the changes to apply.
  • Clocker
    Clocker over 3 years
    Getting unknown configuration python 3.8 on v2020.8.10 of the VSC python extension
  • Neinstein
    Neinstein over 3 years
    @Tiw especially since an other answer suggest the very opposite.
  • WLiu
    WLiu over 3 years
    It works for me and I found that I can remove it after the unresolved import was gone. So, is it the IDE bug ?
  • Shinebayar G
    Shinebayar G over 3 years
    @WLiu If you remove, it may not work again if you relaunch VsCode again. It's not entirely bug, rather implementation issue of VSCode.
  • JEX
    JEX over 3 years
    This helps when you have venv and source separated of course
  • yyFred
    yyFred over 3 years
    Works for me in venv!
  • dillon.harless
    dillon.harless over 3 years
    Glad people are finding this useful, however is there anyone that can explain that second key-value pair?
  • shmim
    shmim over 3 years
    At some point the name of the setting changed. It's now "python.analysis.extraPaths"
  • Jason Harrison
    Jason Harrison over 3 years
    the answer defining "python.autoComplete.extraPaths" worked much better.
  • areed1192
    areed1192 over 3 years
    This worked for me as well, more specifically setting the "python.linting.pylintPath" is what fixed the issue. I never had this issue until I upgraded to Python 3.9. My guess is there is a default pylint path they are using.
  • user3821178
    user3821178 over 3 years
    in case you have other other configs in your workspace, you can add them in the dict object, just above the last } .
  • cryanbhu
    cryanbhu over 3 years
    this worked for me. Because I have a directory structure where each python project I have a new venv, with this i point it to that specific Python in the venv directory for that project
  • davidfrancis
    davidfrancis over 3 years
    This only fixes the warning, not the knock on issues. If I use the python path method then the import warning disappears, and also "drill in" ("goto definition") and "parameter hints" work.
  • William John Holden
    William John Holden over 3 years
    This was my problem. Julia apparently installed its own Python interpreter to ~\.julia\conda\3\python.exe.
  • Ruben Flam-Shepherd
    Ruben Flam-Shepherd over 3 years
    This is what ended up working for me. I had my project with code in a package_name folder nested in an outer package_name folder. Adding just package_name to "extraPaths" (not the absolute path) did the trick.
  • Kyle Delaney
    Kyle Delaney about 3 years
    Installing the Pylance extension immediately fixed it for me
  • Pedro77
    Pedro77 about 3 years
    In Visual Studio 2019. Try to reopen VS after package installation. Worked for me.
  • user3661992
    user3661992 about 3 years
    Another unique combination to get rid of the linting error (code still worked though): 1. Change Python language server in settings from Pylance to something else, and 2. Add "python.autoComplete.extraPaths": ["./path-to-your-code"] to settings.json
  • yem
    yem about 3 years
    Worked for me but I had to put the directory name in quotations.
  • Stealth Rabbi
    Stealth Rabbi almost 3 years
    I have pylance installed and I'm still facing this issue for some projects. And is extraPaths suppose to point to a path? What is "src_file" ?
  • Timo
    Timo almost 3 years
    the python interpreter is in workspace.code-workspace. In windows 10, python installed with the app system, my site-packages is here 'C:\\Users\\User\\AppData\\Local\\Packages\\PythonSoftwareFo‌​undation.Python.3.9_‌​qbz5n2kfra8p0\\Local‌​Cache\\local-package‌​s\\Python39\\site-pa‌​ckages', so I took the interpreter in \User\AppData\Local\microsoft.. instead of program files/windowsapps/python... Still no luck, maybe because I have some pip modules in C:\\Program Files\\WindowsApps\\Python.. as well.
  • Timo
    Timo almost 3 years
    Project root folder in vscode means where the workspace.code-workspace is and all the repos as subdirs? Module path is the path to site-packages?
  • ritesh
    ritesh almost 3 years
    Ignoring the warning is not a good practice, instead you should suggest the solution to fix the root cause.
  • Wick 12c
    Wick 12c almost 3 years
    Agreed. I also tried all the options provided but this worked!
  • Otabek Butcher
    Otabek Butcher almost 3 years
    In that case, I have to change the title of my answer to "How to avoid python pylint warning"?
  • jAC
    jAC over 2 years
    Thanks Surveyor Jr. This actually did not work for me back in the day. So maybe there have been improvements. Thanks for adding this workaround to the thread!
  • Jamie Marshall
    Jamie Marshall over 2 years
    This doesn't work for me either
  • Surveyor Jr
    Surveyor Jr over 2 years
    @JamieMarshall I guess it now depends on OS. I am a Windows user. #Assumption. But hopefully you found an answer by now.
  • Jamie Marshall
    Jamie Marshall over 2 years
    @SurveyorJr - I'm windows as well. It turned out for me, I had to uninstall and re-install pylint.
  • oceceli
    oceceli over 2 years
    This answer solved my problem. Thank you
  • Stetzon
    Stetzon over 2 years
    worked for me in 2022 - I"m using venv in my project so I set the interpreter path to ./bin/python
  • Get Off My Lawn
    Get Off My Lawn about 2 years
    I had to use this: "python.analysis.extraPaths": ["${workspaceFolder}/Source"]