-
Notifications
You must be signed in to change notification settings - Fork 771
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pylance doesn't match Python's behaviour wrt installed packages #859
Comments
I think this is what's going on in #733 as well |
The workspace is always assumed to be the first import root, and that module is importable via that path (as it is within the workspace). Editable installs would help at least resolve the import by another name (as it would add that module's parent instead), but as it stands now I think this is working as we'd expect (and likely matches MPLS/jedi's methods of managing imports, though MPLS has its own problems with being unable to import one module by many names). It seems like you're trying to have an environment where the workspace is not the import root (rare from our experiences), but that would definitely require at least editable installs to figure out. I'm not sure if we would be able to figure out that each folder itself is some package needing to also be an import root as well, without some configuration to do so. |
I think editable installs are orthogonal here (note my repro is just a plain install). After selecting the Python interpreter, I'd expect Pylance to resolve imports the same way the Python interpreter would (when invoked from the workspace root) and prefer the installed package:
|
Hm, I see. It's installed and we should be finding Can you enable trace logging and grab a copy of the search paths we are using? They should be printed at startup. |
|
My opinion is you can't go wrong by matching the behaviour of the interpreter. (At least for mypy, basically any time we do things differently from the interpreter people complain) |
How can I help you resolve the discrepancy? @jakebailey are you able to repro? Does my trace log match yours? |
I figured out the difference. It was my mistake. I'm now able to repro what you're seeing. |
OK, I've had a chance to dig into this more. I'm able to repro the pylance errors that you're seeing. When I activate the virtual environment "env" and run the script ("python script.py"), I receive the following output, which matches the errors indicated by pylance:
When I comment out the first import statement and replace the
Based on my results, pylance is matching the behavior of the interpreter. (Incidentally, I'm using Python 3.9 on Mac OS, but I don't think that makes a difference.) @hauntsaninja, are you seeing different results? |
Yeah, I'm seeing different results:
(note that I edited the script in my first comment to add |
@jakebailey, could you see if you can repro what Shantanu is seeing? |
I took the script and ran it. If I do not activate the environment, I see:
If I activate it, I get your result:
Opening VS Code, running |
Looking at sys.path when I open the interpreter, it of course contains the site-packages for the environment, which contains Unfortunately, our logging doesn't actually output the final list of search paths, only some of the intermediates. |
Unless I'm mistaken, microsoft/pyright@170757b fixed this, correct? @erictraut |
Ah yes, I forgot to mark this bug as fixed. |
Thanks both! :-) |
This issue has been fixed in version 2021.1.3, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202113-27-january-2021 |
Pylance prefers looking at local directories over installed packages, which doesn't match the behaviour of the Python interpreter. Here's my repro script:
After that, open
repo
in VSCode, and select./env/bin/python
as the Python interpreter. Then open upscript.py
to verify Pylance's behaviour.I'm aware of
python.analysis.extraPaths
, but it's pretty unwieldy for my use case (I'd have to add several dozen paths for the repo I'm working on). In any case, I think it's unexpected that Pylance's behaviour doesn't match Python's.Note that changing it to an editable install doesn't help either (although I see from your issues that editable installs aren't yet supported).
Environment data
The text was updated successfully, but these errors were encountered: