Repository navigation
Use the previously specified interpreter for a project regardless of environment #20644
Copy link
Copy link
Closed
Labels
area-environmentsFeatures relating to handling interpreter environmentsFeatures relating to handling interpreter environmentsfeature-requestRequest for new features or functionalityRequest for new features or functionalityneeds community feedbackAwaiting community feedbackAwaiting community feedbackverifiedVerification succeededVerification succeeded
Milestone
Description
Original title: Visual Studio Code doesn't select the correct Python interpreter at launch
[Kubuntu 22.04]
To fix it I have to manually select the first interpreter with the 'star' and 'Recommended' tag from the list, however it appears to be already selected (bottom right 3.10.6 ('.venv':venv)) after launch.
Steps to reproduce:
$ mkdir venvtest
$ cd venvtest
$ python3 -m venv .venv
$ source .venv/bin/activate
$ pip install numpy
$ touch ptest.py
$ code .
Python: Show Language Server Output:
[Info - 1:44:07 PM] (21220) Pylance language server 2023.2.10 (pyright 01758105) starting
[Info - 1:44:07 PM] (21220) Server root directory: /home/username/.vscode/extensions/ms-python.vscode-pylance-2023.2.10/dist
[Info - 1:44:07 PM] (21220) Starting service instance "venvtest"
[Info - 1:44:07 PM] (21220) No configuration file found.
[Info - 1:44:07 PM] (21220) No pyproject.toml file found.
[Info - 1:44:07 PM] (21220) Setting pythonPath for service "venvtest": "/home/username/Desktop/Tests/Python/venvtest/.venv"
[Warn - 1:44:07 PM] (21220) stubPath /home/username/Desktop/Tests/Python/venvtest/typings is not a valid directory.
[Info - 1:44:07 PM] (21220) Assuming Python platform Linux
[Info - 1:44:07 PM] (21220) Searching for source files
[Info - 1:44:07 PM] (21220) Found 1 source file
[Info - 1:44:07 PM] (21220) Background analysis(1) root directory: /home/username/.vscode/extensions/ms-python.vscode-pylance-2023.2.10/dist
[Info - 1:44:07 PM] (21220) Background analysis(1) started
Edit (1): Python: Report Issue...
Type: Bug
Diagnostic data
- Python version (& distribution if applicable, e.g. Anaconda): 3.10.6
- Type of virtual environment used (e.g. conda, venv, virtualenv, etc.): Venv
- Value of the
python.languageServersetting: Default
Output for Python in the Output panel (View→Output, change the drop-down the upper-right of the Output panel to Python)
XXX
User Settings
languageServer: "Pylance"
VS Code version: Code 1.75.0 (e2816fe719a4026ffa1ee0189dc89bdfdbafb164, 2023-02-01T15:29:17.766Z)
OS version: Linux x64 5.15.0-58-generic
Modes:
Sandboxed: No
System Info
| Item | Value |
|---|---|
| CPUs | Intel(R) Core(TM) i5-4570 CPU @ 3.20GHz (4 x 3600) |
| GPU Status | 2d_canvas: enabled canvas_oop_rasterization: disabled_off direct_rendering_display_compositor: disabled_off_ok gpu_compositing: enabled multiple_raster_threads: enabled_on opengl: enabled_on rasterization: enabled raw_draw: disabled_off_ok skia_renderer: enabled_on video_decode: disabled_software video_encode: disabled_software vulkan: disabled_off webgl: enabled webgl2: enabled webgpu: disabled_off |
| Load (avg) | 1, 1, 1 |
| Memory (System) | 15.49GB (10.41GB free) |
| Process Argv | . --crash-reporter-id defc98c6-3228-4e0f-9040-d3ced4598bef |
| Screen Reader | no |
| VM | 0% |
| DESKTOP_SESSION | plasma |
| XDG_CURRENT_DESKTOP | KDE |
| XDG_SESSION_DESKTOP | KDE |
| XDG_SESSION_TYPE | x11 |
Extensions (12)
| Extension | Author (truncated) | Version |
|---|---|---|
| prettier-vscode | esb | 9.10.4 |
| vscode-docker | ms- | 1.23.3 |
| isort | ms- | 2022.8.0 |
| python | ms- | 2023.2.0 |
| vscode-pylance | ms- | 2023.2.10 |
| jupyter | ms- | 2023.1.2000312134 |
| jupyter-keymap | ms- | 1.0.0 |
| jupyter-renderers | ms- | 1.0.14 |
| vscode-jupyter-cell-tags | ms- | 0.1.6 |
| vscode-jupyter-slideshow | ms- | 0.1.5 |
| material-icon-theme | PKi | 4.23.1 |
| LiveServer | rit | 5.7.9 |
(3 theme extensions excluded)
A/B Experiments
vsliv368:30146709
vsreu685:30147344
python383cf:30185419
vspor879:30202332
vspor708:30202333
vspor363:30204092
vslsvsres303:30308271
pythonvspyl392:30443607
vserr242:30382549
pythontb:30283811
vsjup518:30340749
pythonptprofiler:30281270
vshan820:30294714
vstes263:30335439
pythondataviewer:30285071
vscod805:30301674
binariesv615:30325510
bridge0708:30335490
bridge0723:30353136
cmake_vspar411:30581797
vsaa593cf:30376535
pythonvs932:30410667
cppdebug:30492333
vscaat:30438848
vsclangdc:30486549
c4g48928:30535728
dsvsc012cf:30540253
azure-dev_surveyone:30548225
vsccc:30610678
pyindex848cf:30577861
nodejswelcome1cf:30587006
3biah626:30602489
f6dab269:30613381
pythonsymbol12cf:30657549
2i9eh265:30646982
Edit (2): Python Show Output
LSP Notebooks experiment is enabled
LSP Notebooks interactive window support is enabled
VS Code was launched from an activated environment: '.venv', selecting it as the interpreter for workspace.
> conda info --json
Python interpreter path: ./.venv/bin/python
> ./.venv/bin/python -I ~/.vscode/extensions/ms-python.python-2023.2.0/pythonFiles/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2023.2.0/pythonFiles/interpreterInfo.py
> .venv/bin/python -I ~/.vscode/extensions/ms-python.python-2023.2.0/pythonFiles/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2023.2.0/pythonFiles/interpreterInfo.py
> ~/miniconda3/bin/python -I ~/.vscode/extensions/ms-python.python-2023.2.0/pythonFiles/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2023.2.0/pythonFiles/interpreterInfo.py
> ~/miniconda3/envs/django-chapter-1/bin/python -I ~/.vscode/extensions/ms-python.python-2023.2.0/pythonFiles/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2023.2.0/pythonFiles/interpreterInfo.py
> ~/miniconda3/envs/python310/bin/python -I ~/.vscode/extensions/ms-python.python-2023.2.0/pythonFiles/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2023.2.0/pythonFiles/interpreterInfo.py
> /bin/python3 -I ~/.vscode/extensions/ms-python.python-2023.2.0/pythonFiles/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2023.2.0/pythonFiles/interpreterInfo.py
> /usr/bin/python3 -I ~/.vscode/extensions/ms-python.python-2023.2.0/pythonFiles/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2023.2.0/pythonFiles/interpreterInfo.py
Starting Pylance language server.
Python interpreter path: .venv/bin/python
and these two lines are printed if I manually select recommended interpreter to fix the issue:
Python interpreter path: ./.venv/bin/python
> . ./.venv/bin/activate && echo 'e8b39361-0157-4923-80e1-22d70d46dee6' && python ~/.vscode/extensions/ms-python.python-2023.2.0/pythonFiles/printEnvVariables.py
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
area-environmentsFeatures relating to handling interpreter environmentsFeatures relating to handling interpreter environmentsfeature-requestRequest for new features or functionalityRequest for new features or functionalityneeds community feedbackAwaiting community feedbackAwaiting community feedbackverifiedVerification succeededVerification succeeded

Activity
brettcannon commentedon Feb 3, 2023
Can you please attach the output from the Python channel in the Output panel (or get all the details that the
Python: report issuecommand pulls together as covered by https://aka.ms/pvsc-bug)?daeh commentedon Feb 3, 2023
I think I'm having a related issue. Since this last update, all of my projects open with the default interpreter. I can select the right interpreter, and that works for the session. But when VS code is restarted, it doesn't remember the correct interpreter and opens the project with the default. All of the interpreters (including the one that gets defaulted to) are conda environments.
I'm using MacOS: 12.6.3
VS code: 1.75.0
Python extension: v2023.2.0
python.languageServersetting: PylanceOutput of python extension when I open a project and set the interpreter to
enviaa:Output when I reopen the project and it defaults to the
labinterpreter:Output of the Jupyter extension when VS code starts and defaults to
labrather thanenviaa:brettcannon commentedon Feb 3, 2023
Output panel logs would still be appreciated.
daeh commentedon Feb 3, 2023
Brett Cannon (@brettcannon) sorry, updated.
the line
VS Code was launched from an activated environment: 'lab', selecting it as the interpreter for workspace.makes me wonder if the python extension is overriding the workspace interpreter because I activate the
labenv in my.zshrcmaurczz commentedon Feb 3, 2023
Dealing with the same exact issue right now, switching to the pre-release version of the extension seems to have fixed the issue.
zebra-f commentedon Feb 3, 2023
Brett Cannon (@brettcannon)
Updated.
daeh commentedon Feb 3, 2023
fyi it doesn't look like the output from the python extension was automatically included
daeh commentedon Feb 3, 2023
Prerelease v2023.3.10341119 doesn't change the behavior for me
zebra-f commentedon Feb 4, 2023
Update
If I open Visual Studio Code by simply clicking on the icon instead from terminal it will load everything correctly (but only after I manually select recommended interpreter manually), built in terminal will also have activated virtual environment, here is the output:
To be clear what's going on:
I open terminal
I type in terminal
I select manually recommended interpreter that fixes the issue
I close Visual Studio Code
I open Visual Studio Code by clicking on its icon (there is no issue)
I close Visual Studio Code
I open Visual Studio Code by clicking on its icon (there is no issue)
I close Visual Studio Code (you get the point)
I type in terminal again
and now what's interesting, sometimes it will open without the issue, sometimes there will be a problem:
Import "numpy" could not be resolved...but built in terminal will have activated(.venv)and most of the time it will have a problem and terminal won't have activated(.venv). (Edit: this behavior is not related to opening via the icon, It the same by runningcode .again right after selecting the correct interpreter and closing Visual Studio Code.)Also I noticed that when I select correct interpreter after opening via terminal that fixes the import problem, built in terminal won't have activated virtual environment (If I create a new terminal after selecting correct interpreter with the
+sign, it will have activated environment).Mauricio Cruz (@maurczz) can you test it?
karrtikr commentedon Feb 4, 2023
Rafal Zebrowski (@zebra-f) Sorry I don't quite understand the issue with your original bug report #20644 (comment). The Interpreter you intend to select
./.venv/bin/pythonis already selected by the extension (.venvmeans the same thing as./.venv/bin/python). So I'm not sure what is there to fix.karrtikr commentedon Feb 4, 2023
Dae (@daeh) Actually yes, although we only intend to do it when
labis explicitly activated, which is not the case with you. Curious, why do you have.zshrcset to automatically activatelabwhen you intend to use a different environment (enviaa) as your interpreter?You temporarily fix the issue by reverting to previous version of the extension. Follow the prompts from "Extensions: Install Specific Version of Extension..." in the command palette:
zebra-f commentedon Feb 4, 2023
Kartik Raj (@karrtikr) me neither, but as I have said, there is a problem-
Import "numpy" could not be resolved Pylance...and there is also thatCoulndn't start client isortpopup window.On the screenshot the first interpreter from the list won't have this issue (it's not selected, I have to do it manually). After selecting it (check my update), closing Visual Studio Code and reopening it again by clicking on the icon (or being lucky with
code .- again look at update) it will have the first one from the list selected and there will be no issue, the second interpreter, highlighted on screenshot won't be even present.Also if I open Visual Studio Code for the first time (with the problem) and I select manually the first interpreter from the list to fix it I won't be able to select the second one again even thought it will be present.
Here is the output when I get lucky reopening with
code .method without the issue:well only
./.venv/bin/pythonappears to be working correctly.zebra-f commentedon Feb 4, 2023
https://streamable.com/bp75s5 sorry for the quality, but important parts should be visible.
At 0:50 I was lucky (already mentioned in update).
At 1:55 I'm trying to select the second interpreter.
karrtikr commentedon Feb 4, 2023
Thanks, I understand the problem. Pylance and isort are using the wrong path to executable, I'll open the respective issues on their end.
30 remaining items
Only use activated environment from terminal if VSCode was launched v…
karrtikr commentedon Feb 9, 2023
Fix should be out in the pre-release version of the extension, use the following to try it out:
daeh commentedon Feb 9, 2023
prerelease works great for me. Thank you Kartik Raj (@karrtikr) !
ydmykr commentedon Mar 8, 2023
So I have a question. If I want a project to use a specified python interpreter, how do I set up or start the project? I don't seem to find a solution in this article.
brettcannon commentedon Mar 8, 2023
ydmykr you can use
"python.defaultInterpreterPath". If you have more questions, please ask in https://github.com/microsoft/vscode-python/discussions .ydmykr commentedon Mar 9, 2023
Brett Cannon (@brettcannon) However, according to the document about vscode-python,
python.defaultInterpreterPathcan only be used in USER settings. Is there any way to set it in thesettings.jsonin.vscodefolder?I want to be able to set the specified workspace instead of global. I can open a new issue if necessary, thanks alot.
Merge latest upstream python extension
Merge latest upstream python extension