38

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?

zx485
  • 2,170
  • 11
  • 17
  • 24
Julian
  • 483
  • 1
  • 4
  • 4

7 Answers7

49

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. :)

sam7351
  • 506
  • 5
  • 3
19

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

Li-Pin Juan
  • 291
  • 2
  • 6
  • `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 '22 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.

artu-hnrq
  • 125
  • 1
  • 8
A Merii
  • 131
  • 4
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.

0

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
  • 101
  • 1
  • None, sorry! I kind of accepted it, I gues... – Julian Aug 06 '19 at 12:37
  • @Julian - sucks...but if I ever find a solution, I'll be sure to get back! – Chet Aug 06 '19 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 06 '19 at 21:00
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'