Pylint "unresolved import" error in Visual Studio Code
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 + P → Python: Select Interpreter → choose the one with the packages you look for:
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
Related videos on Youtube
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, 2022Comments
-
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 over 5 years
pip install pylint-django
, then in vs code settings add this:"python.linting.pylintArgs": [ "--load-plugins=pylint_django", ],
-
jAC over 5 years@VaibhavVishal unfortunately, i still get the same results (added it to my settings.json file)
-
Anupam Haldkar almost 4 yearsIts actually a VScode problem that detects import package very late , after restarting it is fixed automatically.
-
David Dahan over 3 years@VaibhavVishal this is the only solution that worked for me in the entire thread. Thanks.
-
Tim about 3 yearsMy 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 over 5 yearsThis 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 over 5 yearsNot sure. Please try reloading the window of the vs code ( from shell,
code <project_directory> -r
) or just restart the vscode. -
jAC over 5 yearsYeah tried all that, still doesn't recognize them for some reason. Not sure what is going on with it.
-
ruddra over 5 yearsI am not sure, sometimes it may occur if the workspace directory is not set properly. Please make sure
manage.py
is in root ofworkspace
. Also please make sure the pylint is configured properly -
jAC over 5 yearsyou 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 about 5 yearsIt's better to add a bit of explanation.
-
KowaiiNeko about 5 yearsThanks, that worked perfectly!
"${workspaceFolder}/.env"
PYTHONPATH=FolderName
-
Tomasz Chudzik about 5 yearsEditing 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 almost 5 yearsin my case this did not work but hopefully it helps others with this issue.
-
ted almost 5 yearsyeah that's the spirit, it's not a complicated solution, it's what happened for me :)
-
Tomasz Chudzik almost 5 yearsThere 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 almost 5 yearsThis worked for me. Incase anyone was still wondering in 2019.
-
Admin almost 5 yearsThanks so much! Helped me get past an ages-old problem!
-
nairb almost 5 yearsFor me its the other way around, enabling jedi in settings.json works for me. See more here, stackoverflow.com/a/57269144/2877493
-
Robert almost 5 yearsThanks @TomaszChudzik - Setting: "python.autoComplete.extraPaths": ["./src"] worked like a charm!
-
dillon.harless over 4 years@Safder not for me. using virtualenv
-
dillon.harless over 4 years@jAC can you elaborate a little more? I can't seem to figure this out.
-
dillon.harless over 4 yearsThis 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 over 4 yearsAnother 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 over 4 yearsIt just hides the warning if I click on the file, again it shows up!
-
daisy over 4 yearsI 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 over 4 yearsbest 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 about 4 yearsI have been enabled since the beginning. lucky you
-
greendino about 4 yearsin my case. my python 2 interpreter works fine. just my python 3 not able to cooperate very well
-
Richard Li about 4 yearsThis should be the answer.
-
Richard Li about 4 yearsThe answer with "python.autoComplete.extraPaths" solved my problem with the GitHub link that gives the reason.
-
Avid Programmer about 4 yearsThis 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 about 4 yearsReloading window worked for resolving the issue for my own models.
-
Satya Mishra almost 4 yearsThe unresolved-import for relative imports is clearly incorrect, this gets rid of it.
-
alan23273850 almost 4 yearsHello 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 almost 4 yearsThis 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 almost 4 yearsIs ">" literal or part of a prompt?
-
Peter Mortensen almost 4 yearsWhat do you mean by "previous directory"? Do you mean "parent directory"? Or something else?
-
Peter Mortensen almost 4 yearsWhat 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 almost 4 yearsWhat is "code-runner"? Can you add a reference to it (by editing your answer, not here in comments)?
-
Jeremias Caceres almost 4 yearsI only open VSCode with the console in a directory before the project
-
Mahm00d almost 4 yearsThis was exactly my problem (on mac) and selecting the proper interpreter solved it. Thanks!
-
retrovius almost 4 yearsThis 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 almost 4 yearsDon't forget to reload your vs code window after adding this for the changes to apply.
-
Clocker over 3 yearsGetting unknown configuration python 3.8 on v2020.8.10 of the VSC python extension
-
Neinstein over 3 years@Tiw especially since an other answer suggest the very opposite.
-
WLiu over 3 yearsIt 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 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 over 3 yearsThis helps when you have venv and source separated of course
-
yyFred over 3 yearsWorks for me in venv!
-
dillon.harless over 3 yearsGlad people are finding this useful, however is there anyone that can explain that second key-value pair?
-
shmim over 3 yearsAt some point the name of the setting changed. It's now
"python.analysis.extraPaths"
-
Jason Harrison over 3 yearsthe answer defining "python.autoComplete.extraPaths" worked much better.
-
areed1192 over 3 yearsThis 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 over 3 yearsin case you have other other configs in your workspace, you can add them in the dict object, just above the last
}
. -
cryanbhu over 3 yearsthis 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 thevenv
directory for that project -
davidfrancis over 3 yearsThis 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 over 3 yearsThis was my problem. Julia apparently installed its own Python interpreter to
~\.julia\conda\3\python.exe
. -
Ruben Flam-Shepherd over 3 yearsThis is what ended up working for me. I had my project with code in a
package_name
folder nested in an outerpackage_name
folder. Adding justpackage_name
to "extraPaths" (not the absolute path) did the trick. -
Kyle Delaney about 3 yearsInstalling the Pylance extension immediately fixed it for me
-
Pedro77 about 3 yearsIn Visual Studio 2019. Try to reopen VS after package installation. Worked for me.
-
user3661992 about 3 yearsAnother 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 about 3 yearsWorked for me but I had to put the directory name in quotations.
-
Stealth Rabbi almost 3 yearsI 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 almost 3 yearsthe python interpreter is in
workspace.code-workspace
. In windows 10,python
installed with the app system, mysite-packages
is here 'C:\\Users\\User\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python39\\site-packages', so I took the interpreter in\User\AppData\Local\microsoft..
instead ofprogram files/windowsapps/python..
. Still no luck, maybe because I have some pip modules inC:\\Program Files\\WindowsApps\\Python..
as well. -
Timo almost 3 yearsProject 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 almost 3 yearsIgnoring the warning is not a good practice, instead you should suggest the solution to fix the root cause.
-
Wick 12c almost 3 yearsAgreed. I also tried all the options provided but this worked!
-
Otabek Butcher almost 3 yearsIn that case, I have to change the title of my answer to "How to avoid python pylint warning"?
-
jAC over 2 yearsThanks 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 over 2 yearsThis doesn't work for me either
-
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 over 2 years@SurveyorJr - I'm windows as well. It turned out for me, I had to uninstall and re-install pylint.
-
oceceli over 2 yearsThis answer solved my problem. Thank you
-
Stetzon over 2 yearsworked for me in 2022 - I"m using venv in my project so I set the interpreter path to
./bin/python
-
Get Off My Lawn about 2 yearsI had to use this:
"python.analysis.extraPaths": ["${workspaceFolder}/Source"]