41

I'm currently setting up a python development environment with Anaconda and VS Code.

On my old laptop, I could run python code by pressing Shift+Enter and VS Code would send it to the Python Interactive window. When I do this now, the code gets sent to the Terminal. Shift+Enter is registered as a keyboard shortcut for both sending code to Terminal and for sending it to Python Interactive.

I can run stuff in Python Interactive by right clicking and selecting "Run current file in python interactive window".

What I want is that the selected code is run in Python Interactive by pressing Shift+Enter.

How do I do this?

9 Answers 9

52

I was struggling with the same problem, and finally have found out that it's related to the global settings.

If you go to the Settings( Ctrl + , ) you can see the search bar. There, put in 'Send Selection To Interactive Window' or the like, and click the checkbox of 'Python › Data Science: Send Selection To Interactive Window'. After that, you would be able to run certain lines directly in the Interactive Window.

Hope this could help you. :)

4
  • 4
    "jupyter.sendSelectionToInteractiveWindow": true
    – Andrew
    May 16, 2021 at 0:05
  • This setting does not exist (aynmore) in my VS Code installation (version 1.61.2). It seems to be always enabled now, althought the keyboard binding can be changed.
    – wovano
    Nov 6, 2021 at 9:19
  • Thanks for your answer. It helped me a lot.
    – Leonardo
    Feb 23, 2022 at 13:45
  • 2
    This works for me : "jupyter.interactiveWindow.textEditor.executeSelection": true.
    – nico
    Jul 12, 2023 at 12:13
20

In my case (VS Code 1.47.2), the shortcuts Run Selection in Python Terminal and Run Selection in Python Interactive Windows are in conflict with each other (probably messed up by myself a while ago).

The resolution is straightforward: Go to

File -> Preferences -> Keyboard Shortcuts -> Type run selection.

In the result list, change the keybinding of the shortcuts Run .. in Terminal, Run .. in Interactive Windows and Run Cell to Shift+Enter, Alt+Enter and Ctrl+Enter, respectively, or whatever you like as long as no more conflict shows.

Alternatively, you can also look for all shortcuts with the shift+enter keybiding: Go to

File -> Preferences -> Keyboard Shortcuts -> Type "Shift+Enter".

In the result list, change or delete (right click -> remove keybinding or Delete) the keybinding of the shortcuts that also the same keybinding.

EDIT.

In my updated VS Code (version 1.56.2), I did the following things: go to Settings, search for interactive window, in the resulting left panel choose Jupyter, and finally check the box next to Jupyter: Send selection to interactive window. That's it. One more step for some users (including me) is to modify the keybinding for running selection to your preference. For example, got to keyboard shortcuts, type run selection, you should see a list of keybindings and you may need to redefine them if conflicts exist.

enter image description here

1
  • Run Selection in Python Terminal and Run Selection in Python Interactive Windows are both Shift + Enter by default, this is a possible bug
    – Jeff Bezos
    Oct 14, 2022 at 13:46
3

Press CtrlShift+P

Type in "Preferences: Open Settings (JSON)"

Paste this in the JSON file:

"python.dataScience.sendSelectionToInteractiveWindow": true

and save.

1

same issue here. Shift+Enter was working just fine. I was getting an error that I tried to fix, and since then, the "right click" menu has changed.

When I select code and choose right click,Run File in Python Interactive Window, it works. But when I press shift+enter, I get all kinds of errors.

The error I got initially was "This Python interpreter is in a conda environment, but the environment has not been activated. Libraries may fail to load. To activate this environment please see https://conda.io/activation". Trying to fix it has led to the current state....I'll have toe re-trace my steps to see what went wrong.

Have you had any success yet? Thanks!

3
  • None, sorry! I kind of accepted it, I gues...
    – Julian
    Aug 6, 2019 at 12:37
  • @Julian - sucks...but if I ever find a solution, I'll be sure to get back!
    – Chet
    Aug 6, 2019 at 14:43
  • @Julian - if you right click on VS Code, do you see duplicates on the menu? In my case, the Shift+Enter shortcut is "allocated" (for lack of a better word) to "Run Selection/Line in Python Terminal" AND also to "Run Current Cell and Advance". May be that's what's causing the issue. Just a guess....
    – Chet
    Aug 6, 2019 at 21:00
1

In my case the issue was that I did not have a Python interpreter on my local machine running VSCode - as I am always connecting to a remote kernel. Once I installed Python the desired key bindings started working.

1

In my Visual Studio 3.9.7,

Step 1: File > Preferences > Keyboard Shortcuts

Step 2: Type "run selection" in the search box.

Step 3:a) Right-click on Run selection/Line in the Interactive window > Select "remove keybinding"
b) Right-click on Run selection/Line in Python terminal > Select "remove keybinding"

Step 4: Simply double-click on the required command line and Press the desired key combination [Eg: Ctrl+Enter, Shift+Enter], it will display on the box, and finally press the "Enter" key.

0

For me none of the above worked. But resetting both shortcuts seemed to fix it. To do this go to

Keyboard Shortcuts -> type in: run selection -> right click on both settings named: Run Selection/Line In Interactive Window and Run Selection/Line In Python Terminal -> select Reset Keybinding

for further help this is the when settings for each one:

Run Selection/Line In Interactive Window:

editorTextFocus && jupyter.ownsSelection && !findInputFocussed && !notebookEditorFocused && !replaceInputFocussed && editorLangId == 'python'

Run Selection/Line In Python Terminal:

editorTextFocus && !findInputFocussed && !jupyter.ownsSelection && !notebookEditorFocused && !replaceInputFocussed && editorLangId == 'python'
0

@Julian Same issue for me. Seems like a bug see: https://github.com/microsoft/vscode-jupyter/issues/2444

https://stackoverflow.com/questions/59678010/vs-code-run-selection-line-in-python-interactive-window-not-working-at-all

0

I also had conflicting keybindings, so I deleted the one for "send to terminal". It still wouldn't work, so I tinkered with the when settings, and removing the jupyter.ownsSelection requirement works. I am not 100% sure what this means.

VS Code version 1.85.1

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Dec 28, 2023 at 18:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .