Repository navigation
Wrong python interpreter selected in multiroot workspaces when debugging using ${command:python.interpreterPath} #18482
Copy link
Copy link
Closed
Labels
area-debuggingbugIssue identified by VS Code Team member as probable bugIssue identified by VS Code Team member as probable bug
Description
Issue Type: Bug
Behaviour
Expected vs. Actual
Expected:
- Click on the "Debug" green play button from a multi-root workspace
- The debugger starts normally, and all
importstatements works.
Actual:
- Click on the "Debug" green play button from a multi-root workspace
- The debugger starts, but all
importstatements for packages installed inside the venv don't work.
Steps to reproduce:
Create a workspace with the following files:
workspace/
|- python-bug.code-workspace
|- project1/
| |- project1.py
|- project2/
| |- .vscode/
| | |- launch.json
| |- project2.py
| |- requirements.txt
Here's the content of each files:
python-bug.code-workspace:
{
"folders": [
{
"path": "project1"
},
{
"path": "project2"
}
]
}project1/project1.py:
print('hello world')project2/.vscode/launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "project2",
"type": "python",
"request": "launch",
"python": "${command:python.interpreterPath}",
"program": "${workspaceFolder}/project2.py",
"console": "integratedTerminal"
}
]
}project2/project2.py:
import pytz
from datetime import datetime
tokyo = pytz.timezone('Asia/Tokyo')
time = datetime.now().astimezone(tokyo).strftime("%Y-%m-%d %H:%M:%S")
print(f"Tokyo time: {time}")project2/requirements.txt:
pytz==2021.3
- Open the workspace in VSCode
- Open an integrated terminal in project2
- Run
python3 -m venv venvin the project2 terminal - Run
source ./venv/bin/activatein the project2 terminal - Run
pip install -r requirements.txt - Select the python interpreter for project2 to be the one in the
venvfolder - (Sanity check) Run
python project2.pyin the project2 console, it should run fine - (Trigger the issue) Click on the launch button in the debug pane of VSCode, an exception occurs
Workaround
Setting the python interpreter to ${workspaceFolder}/venv/bin/python in launch.json solves the problem, so it seem that the python plugin select the wrong interpreter.
Diagnostic data
- Python version (& distribution if applicable, e.g. Anaconda): 3.8.9
- Type of virtual environment used (e.g. conda, venv, virtualenv, etc.): Global
- Value of the
python.languageServersetting: Pylance
User Settings
defaultLS: {"defaultLSType":"Pylance"}
downloadLanguageServer: true
envFile: "<placeholder>"
venvPath: "<placeholder>"
venvFolders: "<placeholder>"
condaPath: "<placeholder>"
pipenvPath: "<placeholder>"
poetryPath: "<placeholder>"
languageServer: "Pylance"
linting
• enabled: true
• cwd: "<placeholder>"
• Flake8Args: "<placeholder>"
• flake8Enabled: false
• flake8Path: "<placeholder>"
• lintOnSave: true
• banditArgs: "<placeholder>"
• banditEnabled: false
• banditPath: "<placeholder>"
• mypyArgs: "<placeholder>"
• mypyEnabled: false
• mypyPath: "<placeholder>"
• pycodestyleArgs: "<placeholder>"
• pycodestyleEnabled: false
• pycodestylePath: "<placeholder>"
• prospectorArgs: "<placeholder>"
• prospectorEnabled: false
• prospectorPath: "<placeholder>"
• pydocstyleArgs: "<placeholder>"
• pydocstyleEnabled: false
• pydocstylePath: "<placeholder>"
• pylamaArgs: "<placeholder>"
• pylamaEnabled: false
• pylamaPath: "<placeholder>"
• pylintArgs: "<placeholder>"
• pylintPath: "<placeholder>"
sortImports
• args: "<placeholder>"
• path: "<placeholder>"
formatting
• autopep8Args: "<placeholder>"
• autopep8Path: "<placeholder>"
• provider: "black"
• blackArgs: "<placeholder>"
• blackPath: "<placeholder>"
• yapfArgs: "<placeholder>"
• yapfPath: "<placeholder>"
testing
• cwd: "<placeholder>"
• debugPort: 3000
• nosetestArgs: "<placeholder>"
• nosetestsEnabled: undefined
• nosetestPath: "<placeholder>"
• promptToConfigure: true
• pytestArgs: "<placeholder>"
• pytestEnabled: false
• pytestPath: "<placeholder>"
• unittestArgs: "<placeholder>"
• unittestEnabled: false
• autoTestDiscoverOnSaveEnabled: true
terminal
• activateEnvironment: false
• executeInFileDir: "<placeholder>"
• launchArgs: "<placeholder>"
experiments
• enabled: true
• optInto: []
• optOutFrom: []
insidersChannel: "off"
tensorBoard
• logDirectory: "<placeholder>"
Extension version: 2022.0.1814523869
VS Code version: Code 1.64.0 (5554b12acf27056905806867f251c859323ff7e9, 2022-02-03T04:20:17.224Z)
OS version: Darwin x64 20.6.0
Restricted Mode: No
System Info
| Item | Value |
|---|---|
| CPUs | Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz (8 x 2900) |
| GPU Status | 2d_canvas: enabled gpu_compositing: enabled metal: disabled_off multiple_raster_threads: enabled_on oop_rasterization: enabled opengl: enabled_on rasterization: enabled skia_renderer: disabled_off_ok video_decode: enabled webgl: enabled webgl2: enabled |
| Load (avg) | 3, 3, 3 |
| Memory (System) | 16.00GB (0.13GB free) |
| Process Argv | --crash-reporter-id 2f97d10c-30e9-44a1-ba46-b0cd71465152 |
| Screen Reader | no |
| VM | 0% |
A/B Experiments
vsliv368cf:30146710
vsreu685:30147344
python383cf:30185419
vspor879:30202332
vspor708:30202333
vspor363:30204092
pythonvspyl392:30425749
pythontb:30283811
pythonptprofiler:30281270
vshan820:30294714
vstes263:30335439
pythondataviewer:30285071
vscod805:30301674
pythonvspyt200:30340761
binariesv615:30325510
bridge0708:30335490
bridge0723:30353136
vsaa593cf:30376535
testflightcf:30433671
vsc1dst:30433059
pythonvs932:30410667
wslgetstarted:30433507
vs360cf:30404996
vsrem710:30416614
vscscmwlcmt:30433761
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
area-debuggingbugIssue identified by VS Code Team member as probable bugIssue identified by VS Code Team member as probable bug
Activity
kimadeline commentedon Feb 9, 2022
Hi Guillaume Racicot (@gracicot), thank you for reaching out.
You shouldn't have to specify the
pythonline in your launch.json config, if the virtual environment is selected for that specific folder it should get picked up automatically.${command:python.interpreterPath}is also an incorrect value.What happens if you remove the
pythonline from your launch.json config?gracicot commentedon Feb 9, 2022
Kim-Adeline Miguel (@kimadeline) Seems like it's working without the
pythonline. Strangely, the value${command:python.interpreterPath}works very well with a single root workspace. Thanks for pointing out the problem. I'm just unsure how I'm wrong. The python extension allows us to select the python interpreter from the status bar, I figured I had to choose that interpreter to be in line with what's displayed in the editor.kimadeline commentedon Feb 9, 2022
After digging deeper in the code, we do support
${command:python.interpreterPath}. However, when using it we make an assumption that there will be aworkspaceFolderargument with the path to the workspace folder, and use that to determine the correct Python path:vscode-python/src/client/debugger/extension/configuration/launch.json/interpreterPathCommand.ts
Lines 30 to 42 in 33476c4
This assumption is incorrect, since this argument isn't there. This will result in us trying to get the first Python path we can get our hands on (because of the
undefinedvalue), which is fine in single-workspace scenarios, but, as you noticed, doesn't work so well for multi-root workspaces.tl;dr: It's a bug on our end, thank you for reporting it!
7 remaining items
kimadeline commentedon Aug 16, 2023
I'm not on the Python team anymore, but I'll let them know that it's an acute pain point for you 🙂
karrtikr commentedon Aug 17, 2023
Serge Matveenko (@lig) https://github.com/microsoft/vscode-python/suites/15205238033/artifacts/868956057 fixes the issue described in #18482 (comment), try installing it using
Install from VSIXcommand and see if it helps.Set workspaceFolder in debug config before substituting command varia…
lig commentedon Aug 18, 2023
Kartik Raj (@karrtikr) No effect, sorry. I needed to install code-insiders and I've checked that Python v2023.15.5895828483-dev extension is installed.
It looks like it uses a venv defined in the first folder for anything unconditionally now ignoring any folder specific interpreter configuration.
karrtikr commentedon Aug 18, 2023
I see, the cause stated in #18482 (comment) should be fixed, it's likely that there's another bug in addition to that. As a workaround try removing
in your
launch.jsonfrom all workspace folders, let me know if that helps.lig commentedon Aug 21, 2023
I have only one
launch.jsonfile and it doesn't havepythonkey.lig commentedon Aug 21, 2023
So, now it always uses the first folder's venv for everything except stdlib packages. Using "go to definition" on stdlib packages results in opening a file from system default interpreters' library.
I think I can share my entire workspace with you as it contains only Open Source projects at the moment.
lig commentedon Aug 21, 2023
Or maybe some logs would be enough?
karrtikr commentedon Aug 21, 2023
This issue is related to
${command:python.interpreterPath}, if you don't have"python"set, maybe your issue is different and you're not referencing this command at all? In that case please open a new issue usingPython: Report Issuecommand and we'll be happy to have a look.Btw note you need to create separate
launch.jsonfor each workspace folder if you wish to use selected interpreter for each folder:Each
launch.jsonwill only be run using the folder's selected interpreter in which thatlaunch.jsonfile exists.karrtikr commentedon Sep 6, 2023
I believe the original issue #18482 (comment) should be fixed with #21835, hence I'm closing this. If it isn't please let us know and we'll be happy to have a look again.
[-]Wrong python interpreter selected in multiroot workspaces when using ${command:python.interpreterPath}[/-][+]Wrong python interpreter selected in multiroot workspaces when debugging using ${command:python.interpreterPath}[/+]Set workspaceFolder in debug config before substituting command varia…