Skip to content
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

Wrong python interpreter selected in multiroot workspaces when debugging using ${command:python.interpreterPath} #18482

Closed
gracicot opened this issue Feb 8, 2022 · 13 comments
Labels
area-debugging bug Issue identified by VS Code Team member as probable bug

Comments

@gracicot
Copy link

gracicot commented Feb 8, 2022

Issue Type: Bug

Behaviour

Expected vs. Actual

Expected:

  1. Click on the "Debug" green play button from a multi-root workspace
  2. The debugger starts normally, and all import statements works.

Actual:

  1. Click on the "Debug" green play button from a multi-root workspace
  2. The debugger starts, but all import statements 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
  1. Open the workspace in VSCode
  2. Open an integrated terminal in project2
  3. Run python3 -m venv venv in the project2 terminal
  4. Run source ./venv/bin/activate in the project2 terminal
  5. Run pip install -r requirements.txt
  6. Select the python interpreter for project2 to be the one in the venv folder
  7. (Sanity check) Run python project2.py in the project2 console, it should run fine
  8. (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.languageServer setting: 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

@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label Feb 8, 2022
@karthiknadig karthiknadig added area-editor-* User-facing catch-all bug Issue identified by VS Code Team member as probable bug triage and removed triage-needed Needs assignment to the proper sub-team labels Feb 9, 2022
@kimadeline
Copy link

Hi @gracicot, thank you for reaching out.

You shouldn't have to specify the python line 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 python line from your launch.json config?

@kimadeline kimadeline added the info-needed Issue requires more information from poster label Feb 9, 2022
@gracicot
Copy link
Author

gracicot commented Feb 9, 2022

@kimadeline Seems like it's working without the python line. 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
Copy link

After digging deeper in the code, we do support ${command:python.interpreterPath}. However, when using it we make an assumption that there will be a workspaceFolder argument with the path to the workspace folder, and use that to determine the correct Python path:

public _getSelectedInterpreterPath(args: { workspaceFolder: string } | string[]): string {
// If `launch.json` is launching this command, `args.workspaceFolder` carries the workspaceFolder
// If `tasks.json` is launching this command, `args[1]` carries the workspaceFolder
const workspaceFolder = 'workspaceFolder' in args ? args.workspaceFolder : args[1] ? args[1] : undefined;
let workspaceFolderUri;
try {
workspaceFolderUri = workspaceFolder ? Uri.parse(workspaceFolder) : undefined;
} catch (ex) {
workspaceFolderUri = undefined;
}
return this.configurationService.getSettings(workspaceFolderUri).pythonPath;
}

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 undefined value), 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!

@kimadeline kimadeline added needs PR and removed info-needed Issue requires more information from poster triage labels Feb 9, 2022
@kimadeline kimadeline removed their assignment Feb 9, 2022
Harry-Hopkinson added a commit to Harry-Hopkinson/vscode-python that referenced this issue Feb 16, 2022
kimadeline added a commit that referenced this issue Mar 1, 2022
* Fixed namespace and type errors

* Fetch Jupyter Notebook Interpreters if there are multiple available and fetches the path of the interpreter.

* Complying types to reduce errors

* Ran prettier

* News

* Used prettier on service.ts

* Requirements.txt for Python Action Checking

* Removed pr file check

* News

* News

* Updated cffi version

* Ran prettier --check 'src/client/**/*.ts' 'build/**/*.js' for Formatting Multiple Files

* Fixed client factory error and installed react to fix eslint errors.

* Ran global prettier

* Removed unused imports causing errors

* Updated ciffi

* Mass prettier linting

* Removed check python formatting - it was causing errors

* Linting back

* Removed .eslintignore to fix eslint errors - can be generated again

* Added eslint back

* Reverted back to original changes before my commits.

* Updated news

* Error message if you try to run code with no active text editor opened.

* News

* Used IApplicationShell

* Removed unnecessary import

* Used vscode.window to stop errors.

* Testing

* Return error

* Used IApplicationShell for pop up windows. Fixed spelling mistake

* Revered back to vscode while fixing error

* Use IApplicationShell for Error Messages

* Fixed promise types

* Return Error

* Created new constant for message

* Return a message value

* Return the error window

* Using active editor to remove errors while resolving errors.

* Use appshell for errors

* Use an Interface for running the error messages

* Changed types

* Imported window from vscode

* Use IAppShell to display message for testing

* Fixed old python that fail to lint

* Used linting of interpreterInfo to remove error

* Full Stop

* Formatted python Files

* Linted Files

* Linting

* Revert

* Revert

* Python Files reformatted

* Used an Interface in a type file

* Use IAppShell for testing

* Gitignore gitpod for gitpod users

* Reverted

* Incorporate Testing

* Updated prospector

* Test

* Revert

* Revert

* Localised Testing

* Upgrade vsce

* Revert package and package-lock.json

* Remove window import

* Create a window variable

* Remove any types

* Import window from vscode

* Remove unused imports

* Return Types

* Fixed testing

* Await a call - not the function

* Attempt to fix #18455

* New line at the end of the package-lock.json

* New line for package.json

* Window message to check if Isort has been Downloaded

* Window message

* Scrapped idea of a window pop up

* Revert

* Revert

* If no text pointer is found

* Changed type

* Revert

* Testing

* Ran prettier

* Revert

* Revert

* Remove --live-stream from conda run

* Update news

* Remove unused commands

* Add an Extract Method Command #18518

* Fixed linting

* Reverting

* Revert --live-stream to just fix one issue

* Support multiple line input and indented text

* Updated the package-lock.json by updating dependencies..

* Use let instead of var

* Updated test.ts to pass checks -> fix  ```Import may be converted to a default import``` error

* Revert package and package-lock.json

* Fix lint errors

* Revert

* Select the correct python path if  no workspace path is available #18482

* Remove old news

* More testing and return an empty string if no python path is available..

* New tests

* Removed unused dependencies as stated in #16840

* Revert

* Removed unused dependencies with depcheck

* Brung back the loaders in order to load tests and webpack

* Installed more for tests

* Installed webpack-cli

* Uninstalled webpack so only webpack-cli is installed

* Update package-lock.json

* Removed more dependecies

* Install rxjs-compat

* Revert

* Cleanup

* Installed webpack

* Revert

* Revert

* Removed gulp-rename, sourcemaps, gunzip, chmod and unicode

* Reinstated gulp

* Brang back unicode

* Update package-lock.json

* Remove the vscode-debugadapter and the vscode-debugadapter-testsupport

* Removed vscode telemetries.

* Revert before Merge

* Revert

* Merge

* Made changes as requested

* Updated ts-check

* Revert Select the Correct Python Path if no workspace path is available.

* Revert

* Update workspaceFolderUri type after reverting PR

* Revert vscode packages in use

* Merged Changes and Added Back untildify

* Revert

* Revert to PR

* Update package-lock.json

* Added unicode

* Added unicode

* Revert

* Removed the Unused Dependecies, Typescript Char and Typescript Formatter from the Package and Package-lock.json

* Update news

* Update news entry

Co-authored-by: Kim-Adeline Miguel <kimiguel@microsoft.com>
@github-actions github-actions bot removed the needs PR label Aug 9, 2022
@karrtikr karrtikr added the needs PR Ready to be worked on label Aug 9, 2022
@karrtikr karrtikr added area-debugging and removed area-editor-* User-facing catch-all labels Dec 20, 2022
@lig
Copy link

lig commented Aug 9, 2023

@kimadeline I've been facing this really hard recently. A multi-root workspace keeps using an interpreter defined for the first folder instead of the correct one.

Bounty opportunity: I'm ready to pay €20 for this to be fixed.

@kimadeline
Copy link

I'm not on the Python team anymore, but I'll let them know that it's an acute pain point for you 🙂

@karrtikr
Copy link

@lig https://github.com/microsoft/vscode-python/suites/15205238033/artifacts/868956057 fixes the issue described in #18482 (comment), try installing it using Install from VSIX command and see if it helps.

@lig
Copy link

lig commented Aug 18, 2023

@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
Copy link

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

"python": "${command:python.interpreterPath}",

in your launch.json from all workspace folders, let me know if that helps.

@lig
Copy link

lig commented Aug 21, 2023

I have only one launch.json file and it doesn't have python key.

@lig
Copy link

lig commented 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
Copy link

lig commented Aug 21, 2023

Or maybe some logs would be enough?

@karrtikr
Copy link

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 using Python: Report Issue command and we'll be happy to have a look.

Btw note you need to create separate launch.json for each workspace folder if you wish to use selected interpreter for each folder:

image

Each launch.json will only be run using the folder's selected interpreter in which that launch.json file exists.

@karrtikr
Copy link

karrtikr commented 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.

@karrtikr karrtikr closed this as completed Sep 6, 2023
@github-actions github-actions bot removed the needs PR Ready to be worked on label Sep 6, 2023
@karrtikr karrtikr changed the title 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} Sep 6, 2023
anthonykim1 pushed a commit to anthonykim1/vscode-python that referenced this issue Sep 12, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-debugging bug Issue identified by VS Code Team member as probable bug
Projects
None yet
Development

No branches or pull requests

5 participants