-
Notifications
You must be signed in to change notification settings - Fork 889
Description
Overview
Specifically, if I directly open (i.e., from the File Explorer) e.g. .venv/lib/python3.13/site-packages/sklearn/base.py
(as opposed to navigating to it via Go To Definition), then I get the following error from Pylance regarding the numpy
import:

This seems to happen systematically:
- in multi-root workspaces (note there's a
bbb
root in the screenshot, above theaaa
root whose virtualenv we're rummaging in) - only in library code inside the virtualenv (not user code)
- when opened directly from the File Explorer (not via Go To Definition)
- for all non-stdlib absolute imports (as you can see in the screenshot, stdlib absolute imports above the
numpy
import resolve fine, as do relative imports below it)
Additionally, if I import sklearn.base
in user code and use Go To Definition (F12) on it...

... then the error goes away and Pylance resolves the numpy
import correctly from that point onwards (at least until the language server is restarted):

Detailed reproduction steps
Automated setup
Please install uv first.
uv init aaa
cd aaa
uv add scikit-learn
cd ..
uv init bbb
echo '{"folders":[{"path":"bbb"},{"path":"aaa"}],"settings":{}}' >test.code-workspace
code --profile $(basename $(mktemp --dry-run)) test.code-workspace
Manual steps
The last command in the automated setup will open a VSCode window using a fresh profile, with a workspace with two folders, aaa
and bbb
. Then:
- Under the
aaa
root, navigate to.venv/lib/python3.13/site-packages/sklearn/base.py
in the File Explorer and double-click on it. - A VSCode popup will show, suggesting to install the Python extension. Accept the suggestion.
- Go back to the opened file. The
import numpy as np
line should be underlined with a yellow squiggle, with Pylance reporting: "Import "numpy" could not be resolved PylancereportMissingImports".
Why is this a bug?
In other situations, Pylance behaves correctly and resolves the numpy
import, even when the file is opened directly. For instance, when not using workspaces at all:

Or when using a single-root workspace:

Which leads me to think that at least aspirationally, it should work in multi-root workspaces too :)
I'm aware of microsoft/vscode-python#21204, which says:
Current hypothesis is that our multiroot support isn't great, so solving that might solve the issue for mono repos.
So I guess this falls under the tangled issue of better multiroot support, and might not be solvable on its own / might need to be part of a larger refactor. But I think it's still worth it to report it, at least as one of the things to test after the large refactor (if that's indeed how it goes).