microsoft / vscode-python

Python extension for Visual Studio Code
https://aka.ms/pvsc-marketplace
MIT License
4.33k stars 1.19k forks source link

Switching Kernel/Interpreter in Python Native REPL dropdown forces use of IPyKernel. #23971

Open szymonhennel opened 2 months ago

szymonhennel commented 2 months ago

Type: Bug

Behavior

Context

Upon starting a native python REPL, the default kernel "Python REPL" is active. With the setting "python.REPL.sendToNativeREPL": true, Shift+Enter sends code from the editor to the REPL. If the user is happy working with the default kernel, everything is fine an no issues occur. If the user wants to use a different kernel, this can be achieved by clicking on the kernel button in the top right corner of the native REPL pane. The kernel selection works as expected, and the selected kernel remains active without issues as long as code is entered and executed manually.

Where the bug manifests

The bug is that when a non-default kernel is selected and code is sent to the REPL from the editor pane by Shift+Enter, this action triggers a change of the kernel back to "Python REPL", and the code sent is interpreted by this default kernel. This makes the setting "Send to active REPL" unusable if the user works with a virtual environment.

Expected behavior

The kernel selection should not be affected by sending code to the REPL.

Environment

Python 3.12.5 on Windows 11 Pro 23H2, virtual environment managed by poetry, python extension v2024.12.3.

User settings

I have checked that any combination of the settings "interactiveWindow.executeWithShiftEnter", "python.terminal.activateEnvInCurrentTerminal" and "python.terminal.activateEnvironment" do not affect the behavior described above.

How to reproduce:

And since this is my first time interacting with this community I take this opportunity to thank you all for providing this awesome extension.

anthonykim1 commented 2 months ago

Hello @szymonhennel Thank you for filing the issue and I truly appreciate your kind words and appreciation here. You made great points here and I appreciate the detailed info here.

Couple things here, but let's try to unwrap and discuss this step by step:

  1. I will bring this up to the Python team and folks in Jupyter.
  2. The big intention and motivation behind Native REPL feature was to provide Python user to access an enhanced REPL (with features such as intellisense and autocomplete) that the Terminal REPL did not have. All of this without having to install Jupyter extension nor iPyKernel that you need in order to launch and experience the Jupyter notebook or Interactive Window Option to run your Python code. This way, users just need their "basic" Python interpreter to test out their code quickly without having to sit and wait for iPykernel to be installed which is coming from Jupyter side of things.
  3. That being said, the very moment you decide to pick a "kernel" yourself instead of Python REPL, it seems that Jupyter extension switches your mode from Native REPL(Provided by Python extension) to Interactive Window(one provided by Jupyter). The way you know this is the output: Basically if you are in the lightweight native REPL mode, you will not see colorized output but rather output generated by Python extension's REPL server. Screenshot 2024-08-26 at 4 02 41 PM

If you are in the interactive window mode, you will see colorized output from the iPyKernel itself coming from Jupyter:

Screenshot 2024-08-26 at 3 59 59 PM
  1. It looks like you are satisfied enough with the output that native REPL generates and do not necessary need the one from Jupyter. If that is the case, this can be a great feature request idea that you can post with the 'feature request' tag. I can see this being totally valid request.

  2. I am also wondering if you can change interpreter via command palette (ctrl/cmd + shift + p) 'Select Interpreter' and try opening the Native REPL again and see your desired effect? Instead of changing the kernel via clicking on 'Python REPL'

szymonhennel commented 2 months ago

Hi @anthonykim1 thank you for answering and sorry for taking a long time to come back to you, I will answer by tomorrow.

kirill-dobryakovv commented 1 month ago

Hey @anthonykim1, thank you for the context! Came across the same issue, happy it was brought up! To answer your question, changing the interpreter via the command palette (ctrl/cmd + shift + p) has zero effect on the issue, it is still reproducible.

It seems like what needs to be fixed here is the command that sends the code over to the interactive window, everything else works fine.

I personally think that using a Jupyter-provided Interactive Window has a nicer workflow than the ipynb.

Tagging @IanMatthewHuff as an engineer involved in making this feature.

amunger commented 1 month ago

the very moment you decide to pick a "kernel" yourself instead of Python REPL, it seems that Jupyter extension switches your mode from Native REPL to Interactive Window

IIUC, this is what the user is asking for, but doesn't quite work - you can change the kernel, but using sendToNativeRepl will switch the kernel back to python's.

The user should be able to select a different kernel to use here, and would actually provide a workaround for https://github.com/microsoft/vscode-jupyter/issues/15988

Recording 2024-10-04 at 14 03 59

anthonykim1 commented 1 month ago

I see, with the explanation + video made it super clear. Thanks!

The smoothest transition I can think of would be: Perhaps we need to detect when user has switched their kernel from (native REPL to IW) and automatically change the keybinding (of shift+enter) to send to IW instead of Python Native REPL?

If we detect user change their kernel back to native REPL (Im not sure if this is possible), we would again change/revert the shift+enter keybinding to send command to native REPL instead of IW.

Does this sound reasonable?

amunger commented 1 month ago

I think a much simpler solution would just be to not run the selectKernel command if the editor was already opened - allow the user to use a different kernel in the python REPL editor.

HansAtonenGS commented 1 month ago

I was facing a similar issue, but I managed to change it so it doesn't use the Python REPL editor every time I press Shift+Enter by doing the following:

TL;DR: Change jupyter.interactiveWindow.textEditor.executeSelection to true using the Visual Studio Code Settings UI. This made it start using my kernel as the default option.

Steps: Open Settings UI:

Search for the Setting:

Enable the Setting:

github-actions[bot] commented 3 days ago

Hey @anthonykim1, this issue might need further attention.

@szymonhennel, you can help us out by closing this issue if the problem no longer exists, or adding more information.