vscode-python: [Language Server] "Unable to resolve module" for app project
Environment data
- VS Code version: Version 1.25.1 (1.25.1)
- Extension version (available under the Extensions sidebar): 2018.7.0
- OS and version: macOS 10.13.6
- Python version (& distribution if applicable, e.g. Anaconda): 3.6.3
- Type of virtual environment used (N/A | venv | virtualenv | conda | …): virtualenv
- Relevant/affected Python packages and their versions: XXX
Actual behavior
I have a few directories that form my python workspace. Each directory is a separate python project (either app, that’s executed, or library, that has setup.py and is installed) with a separate env configured inside vscode. I’ve turned on new “language server” feature.
Imports from my app are underlined and say “Unable to resolve <module>. Intellisense may be missing.” Imports from my libs work fine.
Expected behavior
I’d like language server to process apps as well (autocomplete, goto, etc.)
Logs
Starting Microsoft Python language server.
[Info - 10:43:22 PM] Created Microsoft.PythonTools.Interpreter.Ast.AstPythonInterpreter instance from Microsoft.PythonTools.Interpreter.Ast.AstPythonInterpreterFactory
Initializing for /Users/yevhen/.virtualenvs/<app_project>/bin/python
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 23
- Comments: 42 (9 by maintainers)
I’m also having the same issue.
All relative imports within my own code (non-3rd party) are marked as unable to resolve module. Jedi handles the code correctly.
Logs
I got this down to a minimal set of steps:
Create a project structure like this
package
is a package with an empty__init__.py
file.test.py
tries toimport package
.env
is a virtual environment (python3 -m venv env
)Open this folder in fresh install of VSCode 1.25 and vscode-python 2018.7.0 as a single-folder workspace (just
Open folder
), with Jedi disabled. Set the Python interpreter to the one in the virtual environment.You’ll see the “IntelliSense may be missing” message for the
import package
line.To be honest, the virtual environment doesn’t make a difference, I just included it because it’s one more place to look for the package in addition to the system libs and our own packages, and it isn’t working either. I tested this on Debian 9, but I don’t think it makes a difference (others have different setups). Import, auto-completion and signatures for global packages (e.g.:
datetime
,json
) work just fine.As I said in my previous message, adding folders manually in
python.autoComplete.extraPaths
makes it work.I have verified the issue as described by @alnvdl, summarized in this gif:
…and as @MsLink mentioned, the issue is corrected when you visit the init.py file within the packages subfolder.
The problem seems to be caused by the fact that the language server is not looking for modules inside virtual environment paths. For more details, look at how
searchPath
is formed inlanguageServer.ts
.To workaround the issue, set
python.autocomplete.extraPaths
either in the general VSCode settings or in the workspace settings to something like this (for some reason, it didn’t work when I set it in the folder settings):This is obviously not ideal, but it should get the job done while the developers work on a fix, assuming we’re not doing something wrong in our workspace setup 😄. By the way, I see this problem for both 3rd party modules I install in the virtual environment, as well as for my own modules inside the project folder in a workspace. It works fine with Jedi.
For the developers: I found this through the
AnalysisLog.txt
file, at least for me it was just loading whatever paths were indatabase.path
and nothing else. I couldn’t editdatabase.path
because it was overriden every time I reloaded the window, but I think that’s by design.I have the same issue. Everything works well with
jedi
but not with the language server. SettingPYTHONPATH
andpython.autoComplete.extraPaths
didn’t make sense.I use both relative imports and absolutely imports (all my own code), they all are marked “Unable to resolve”. jedi works well too.
BTW, how could I prevent language server? I tried to delete ~/.vscode/ms-python/languageServer and switch “python.jediEnabled” config on and off, but it still will re-download. And I’m at work now…😷
env:
log:
I can’t reproduce with the latest language server:
If you want to download a development build to test this you can, otherwise this will be available in our next release which should be out hopefully this week.
I tried the example from @alnvdl and disabled all the extensions except python and language pack, yet it replicated.
Here’s what I do:
package/__init__.py
If I open
pakcage/__init__.py
all things are fine, but it’s difficult to do it for every file in a large project, especiallyReload Window
command will let them go back to origin.For the situation of @PowerSnail, I don’t use pylint but I also disable flake8 and formatter, no help for me sadly.
My config:
Just want to report an interesting phenomenon:
I encountered the same phenomenon. However, with pylint disabled, and after I clicked through every file in the project, the problem disappeared. The intellisense works: autocomplete, fetching the docstring of methods, etc.
Is it possible that this is a problem of Language server interacting with pylint?
Update: Autocomplete still works only sometimes…
I have a similar issue with an interesting finding. I have main.py and hi.py in the same folder. When I opened main.py for the first time, the module
hi
couldn’t be resolved. Then I opened the hi.py manually, it can be resolved.hi.py
main.py
@0x1EE7 can you open a new issue?
And it looks to me that @eikonomega 's problem is similar. He opened his app project and server could not process it. Initially I reported the same thing, maybe my comments about multiple projects and virtualenvs were misleading.
@yevhen-m does everything work as expected when you use Jedi as the intellisense engine? And are you saying you have a env file that sets up your
PYTHONPATH
to make everything find each other? If so can yu provide your env file?