49

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?

Giacomo1968
  • 58,727
Julian
  • 593

10 Answers10

63

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.

Giacomo1968
  • 58,727
sam7351
  • 646
26

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

3

Press CtrlShift+P

Type in "Preferences: Open Settings (JSON)"

Paste this in the JSON file:

"python.dataScience.sendSelectionToInteractiveWindow": true

and save.

artu-hnrq
  • 125
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!

Chet
  • 111
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

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

0

On VSCode 1.98.2 it's now found under:

VSCode 1.98.2 jupyter interactive window settings

which translates into the following line in the settings.json:

"jupyter.interactiveWindow.textEditor.executeSelection": true
s.k
  • 161
  • 1
  • 3
  • 9