Python Unresolved import issue
When pylinting a file in a big project, it appears as though libraries installed in a venv are not being discovered and thus I'm getting unresolved import issues.
Version: Visual Studio Code Version 1.33.0 Python 2.7.14 (which is what I have to use in my venv)
Sadly, I can't post a screen shot of the issues due to proprietary restrictions. However, I am able to tab over to the Terminal window, which is pointed to my venv and open python and import each of the libraries that the Problems view indicates are problems from the linting operation without issues.
These aren't pylint errors either they are "Python(unresolved-import)" issues. Neither pytlint, pep8, or flake8 report issues with those imports.
Seems similar to https://github.com/Microsoft/vscode-python/issues/3840 which was flagged as being closed, but doesn't appear to be resolved, and I can't post there as that issue is locked.
Could also be simlar to https://github.com/Microsoft/python-language-server/issues/541 which is 'closed' and https://github.com/Microsoft/python-language-server/issues/887 which made me spawn this issue.
I'll see if I can reproduce in more a basic way without proprietary libraries in the venv so that it can be fixed and add here with the details.
Is there any news on this? I'm having the same issue 😞
I'm going through this and related issues, and for the most part you can fix things by setting extraPaths to tell us more directories to use as import roots, for example:
{
"python.autoComplete.extraPaths": [
"./src"
]
}
If you have more folders, you can list them all, and things should work.
However, this doesn't seem to work when a package gets installed as editable. I believe this has something to do with how the interpreter's own default search paths get modified and how we merge all of this configuration together. I'm currently just going through all of the test projects everyone has provided to get a good view on the unresolved imports first before trying to nail that one.
At the moment, our current recommendation is to use the extraPaths configuration to point the LS to your import roots. See the new documentation here: https://github.com/microsoft/python-language-server/blob/master/TROUBLESHOOTING.md#unresolved-import-warnings
A fix to editable installs was merged in #1183 (v0.3.1+), and will be available in the daily download channel shortly. To switch to this for testing, you can set:
"python.analysis.downloadChannel": "daily"
@jakebailey Thanks for the update on the fix! :D
Is there an estimate for when this might make it to the default/stable channel? Do I need to be on an Insiders build to use the daily download channel?
It'd also probably be helpful, to update https://github.com/microsoft/vscode-python/issues/3840 to link here.
Probably within a couple days. There are some changes in 0.3.0+ that we're trying to ensure aren't causing other issues.
You do not need the insiders build to run the daily channel, just place the config in your settings and reload.
@pradyunsg try the daily channel with the fix for editable installs. I've been using it for about a week and everything works nice and smooth. Of course, don't forget to add your library to extraPaths.
@jakebailey When I try and add "python.analysis.downloadChannel": "daily", to my settings.json, I'm getting this error: "Unknown configuration setting." Am I putting it in the wrong place?
No; it's an undocumented option that we recommend for testing, so VS Code will say that it doesn't exist. If it's present, it will be used.
That worked for my imported libraries, but it's not working for my local modules in the same containing folder. Ex: My current file is /core/posts/views.py, and my import from posts.models import Post causes posts.models to show an Unresolved Import.
Removing the current folder's name fixes it; Ex: from .models import Post, but that's negatively impacting readability, since I've got multiple models files to pull from (like core/users/models.py).
Adding the master containing folder in front of the imports seems to work, but then it breaks at run-time. Ex: from core.posts.models import Post removes the error, but breaks everything.
Did you add ./core to your extraPaths?
I did; I even tried adding each sub-folder to it (in a few different ways, because I was getting desperate and was willing to try anything). Here's all the stuff I added to extraPaths:
"python.autoComplete.extraPaths": [
"./core",
"./core/posts",
"./core/users",
"./posts",
"./users"
],
Is that formatted correctly? I know I don't need both "./core/posts and "./posts", but I was just trying anything to see if it helped.
Of those, I think only ./core should be required... Definitely not the last two, but unless you're relying on absolute imports of both posts.something and something, you should only have the one. Anything past that seems very strange.
I was wondering, regarding the editable installs, if python can find those modules, why can't the language server and I need to add them to the extra paths?
I can live with that, but it can get annoying if, as me, you work with a lot of editable installs and switch back and forth between editable and packaged.
That's what I thought. I've tried both of these configurations (separately) with no luck:
"python.autoComplete.extraPaths": [
"./core",
"./core/posts",
"./core/users",
],
"python.autoComplete.extraPaths": [
"./core",
],
@egabrum
Editable installs use pth files, which are pretty powerful (can execute any python code at startup before any user code ever runs, but usually just contain paths) and are hard to deal with from an analysis point of view. All we can do right now is ask the interpreter "what are your paths", but the interpreter tells us this editable-installed code is a site package (because that's how pth files work), hence a configuration to tell us "no, actually this is my code".
I'd like to try and remove the extraPaths requirement, but to do that I'm going to have to research a way to distinguish what pth files are just modifying paths for the user code, which ones are doing things for libraries (matplotlib ships one which does some funky stuff), and so on, with potentially more issues. Just assuming that anything that is within the workspace is user code will not work; we can't just assume any path that lives in the workspace is user code (people put venvs in the same dir as their code).
The language server is not the interpreter. It doesn't execute the code, doesn't know if some file is supposed to be a script (and has special paths added on), can't handle most at-runtime sys.path modifications, etc, and must do everything statically. I opened #1213 to track trying to improve our search path handling, but it's not as simple as "if python can do it then so can we" when we aren't actually executing the code, just trying to provide suggestions by viewing files.
@mattlefevre I'd appreciate it if you could enable trace logging and upload a copy of your logs. If your code is somewhere to test, that would be extremely helpful. I mainly want to see the search paths reported.
Here's the repo I'm working on: https://github.com/mattlefevre/book-blog
I briefly looking into how to enable trace logging, but all I could find is how to use the debugger in VSC (still useful, but not what I was looking for).
@jakebailey if the main concern is people putting virtualenv in the project folders and getting confused on that, would it make sense to statically try to detect whether a path is in one?
@mattlefevre See: https://github.com/microsoft/python-language-server/blob/master/TROUBLESHOOTING.md#filing-an-issue I'll look at that test case tomorrow and see what's going on. (Everyone who's provided test cases for the other path issues are fixed, but it's the more complicated cases where conveniently people can't share the code... 😛 )
@pradyunsg Probably, but it's a bit tough to determine what is the interpreter's path. If an editable install is performed, then it will start saying things normally outside of its own path is "interpreter". It's going to require research and a big list of cases to get that sorted out. Note that if people are writing packages and not using editable installs, then the language server is going to have no way at all of knowing that imports are relative to another directory. It might be the case that extraPaths is required no matter what.
On a perfectly working setup (using extraPaths) , this keeps happening after having vscode open for a few days.
Big clue is: Reloading the window fixes it!
I've got a crazy amount of log information at this point (yay!), and I don't think pasting it all in here would work out (the initial loading of VSC is about 3500 lines). I'm not sure exactly what you need, @jakebailey , but here's all of the initial informational logs:
Starting Microsoft Python language server.
Downloading https://pvsc.azureedge.net/python-language-server-daily/Python-Language-Server-win-x64.0.3.18.nupkg... ##########Linting Output - pylint##########
complete
Unpacking archive... ##########Linting Output - pylint##########
done
[Info - 3:59:16 PM] Analysis cache path: C:\Users\mlefevre\AppData\Local\Microsoft\Python Language Server
[Info - 3:59:17 PM] GetCurrentSearchPaths C:\Users\mlefevre\.virtualenvs\ambersite-fSB87Qgi\Scripts\python.exe
[Info - 3:59:17 PM] Interpreter search paths:
[Info - 3:59:17 PM] \python
[Info - 3:59:17 PM] \python\pyspark
[Info - 3:59:17 PM] c:\users\mlefevre\.virtualenvs\ambersite-fsb87qgi\lib
[Info - 3:59:17 PM] c:\users\mlefevre\.virtualenvs\ambersite-fsb87qgi\scripts
[Info - 3:59:17 PM] c:\users\mlefevre\appdata\local\programs\python\python37-32\lib
[Info - 3:59:17 PM] c:\users\mlefevre\appdata\local\programs\python\python37-32\dlls
[Info - 3:59:17 PM] c:\users\mlefevre\.virtualenvs\ambersite-fsb87qgi
[Info - 3:59:17 PM] c:\users\mlefevre\.virtualenvs\ambersite-fsb87qgi\lib\site-packages
[Info - 3:59:17 PM] User search paths:
[Info - 3:59:29 PM] Microsoft Python Language Server version 0.3.18.0
[Info - 3:59:29 PM] Initializing for C:\Users\mlefevre\.virtualenvs\ambersite-fSB87Qgi\Scripts\python.exe
Were there any other specific logs I should include here? Or did you want me to attach the entire set of initial logs as a .txt file?