Pylance using the wrong interpreter
Hi! I'm working on a VSCode extension adding support for Pixi, and I can't get Pylance to use the right interpreter when having two or more Python projects in the same single-root workspace. I initially thought it was a bug in my code, but I can reproduce it with uv using the built-in managers.
My VSCode and extensions are all up-to-date, and this happens on both release and stable versions of the Python extension pack. You can see in the video that only Pylance has a problem when I switch between main.py files, complaining about a missing import error. Executing the file works just fine.
My local settings.json:
{
"python.useEnvironmentsExtension": true,
"python-envs.pythonProjects": [
{
"path": "nested",
"envManager": "ms-python.python:venv",
"packageManager": "ms-python.python:pip"
}
],
}
https://github.com/user-attachments/assets/8f490f4b-9aca-4377-bcbf-116fe30b57c7
I will take a look- thanks for the detailed info
Able to reproduce on my side - seeing both the fact that runs works but pylance is not working. Given the fact that run is selecting the right environment I am thinking this might be a pylance issue with the new extension. Let me transfer it over there and see what the pylance team thinks- thanks!
here is a zip of the folder I used to test
Hi @renan-r-santos, I'm an AI Support assistant here to help with your issue. While the team reviews your request, I wanted to provide some possible tips and documentation that might help you in the meantime.
Similar Issues (FYI only, not solutions)
The following issues may or may not be related to your report. They are provided for reference only, as they may describe similar symptoms, affect the same feature, or address related concerns, but may not be the same issue.
- Wrong interpreter found (Pylance uses system Python instead of venv) https://github.com/microsoft/pylance-release/issues/904
- Pylance marks imports as unresolved when wrong interpreter selected https://github.com/microsoft/pylance-release/issues/3707
- Imports can’t be resolved because Pylance isn’t using the virtual environment https://github.com/microsoft/pylance-release/issues/1791
Potential Workarounds
- Use the VS Code command palette (
Ctrl+Shift+P) → “Python: Select Interpreter” and explicitly choose your project’s virtual environment so that Pylance picks up the correct interpreter.
The team will respond to your issue shortly. Please note this is a trial feature and may not be fully accurate. I hope these suggestions are helpful in the meantime. If this comment helped you, please give it a 👍. If the suggestion was not helpful or was incorrect, please give it a 👎. Your feedback helps us improve!
side note- thank you so much for working on an extension to add support for pixi! 🎊
adding a pyrightconfig.json with two environments listed might fix this issue but i'm not sure nested is supported.
{
"executionEnvironments": [
{
"root": "./projectA",
"pythonPath": "./projectA/.venv/bin/python"
},
{
"root": "./projectB",
"pythonPath": "./projectB/.venv/bin/python"
}
]
}
pythonPath doesn't seem to be a valid pyright/Pylance config option, so unfortunately that doesn't help.
Are there any plans to make Pylance use the Python Environments API to get and use the right interpreter? Now that https://github.com/microsoft/vscode-python-debugger/issues/835 is merged, this seems to be the only Python extension blocking VSCode users from using the Single Workspace, Multiple Environments (1:N) structure as mentioned by @karthiknadig in https://github.com/microsoft/vscode-python-environments/issues/378#issuecomment-2848249392
cc @eleanorjboyd
I believe this is the same request as this: https://github.com/microsoft/pylance-release/issues/5564
Or at least the way we'd implement the support would be the same.