I just made the transition from Spyder to VScode for my python endeavours. Is there a way to run individual lines of code? That's how I used to do my on-the-spot debugging, but I can't find an option for it in VScode and really don't want to keep setting and removing breakpoints.
-
Possible duplicate of [VS Code execute current line or selection to in the integrated console](https://stackoverflow.com/questions/45667252/vs-code-execute-current-line-or-selection-to-in-the-integrated-console) – Mark Apr 11 '18 at 13:44
-
I have looked up the possible duplicate, the question is a duplicate, the answers are not: they are still suggesting adding macros, it is at an old stage of mid 2017, which might rather confuse any users of today. This question again is outdated, as in some answers, the shortcut seems yet to come, though it is already there, also with the output popping up automatically. The answer of @ZacharyRianSmith should be accepted. – questionto42 Jul 28 '20 at 06:04
-
very similar question, but not Python-specific (these two questions are different because of that): [How can I run text selected in the active editor in VS Code's integrated terminal?](https://stackoverflow.com/q/38952053/11107541) – starball Aug 31 '23 at 18:25
6 Answers
If you highlight some code, you can right-click or run the command, Run Selection/Line in Python Terminal
.
We are also planning on implementing Ctrl-Enter to do the same thing and looking at Ctr-Enter executing the current line.

- 14,438
- 3
- 45
- 40
-
Yeah but if I use this i am not able to see what values my individual variables are taking? I am in the exact same position as OP, and I am looking to run snippets of code while being able to see what values the variables are taking is that not possible? – no nein May 06 '20 at 06:51
-
You want the Interactive Windows which has a variable explorer. Then you can run code line-by-line. – Brett Cannon May 06 '20 at 22:18
You can:
- open a terminal at Terminal>New Terminal
- Highlight the code you want to run
- Hit Terminal>Run Selected Text
As for R you can hit CTRL Enter
to execute the highlighted code. For python there's apparently no default shortcut (see below), but I am quite sure you can add yours.

- 2,094
- 2
- 19
- 37
In my ver of VSCode (1.25), shift+enter will run selection. Note that you will want to have your integrated terminal running python.

- 2,688
- 1
- 20
- 30
-
-
Are you aware of a way to execute it in the debug console instead of the terminal? if i set a breakpoint, the terminal doesn't have any context. – citynorman May 08 '21 at 03:41
I'm still trying to figure out how to make vscode do what I need (interactive python plots), but I can offer a more complete answer to the question at hand than what has been given so far:
1- Evaluate current selection in debug terminal is an option that is not enabled by default, so you may want to bind the 'editor.debug.action.selectionToRepl' action to whatever keyboard shortcut you choose (I'm using F9). As of today, there still appears to be no option to evaluate current line while debugging, only current selection.
2- Evaluate current line or selection in python terminal is enabled by default, but I'm on Windows where this isn't doing what I would expect - it evaluates in a new runtime, which does no good if you're trying to debug an existing runtime. So I can't say much about how useful this option is, or even if it is necessary since anytime you'd want to evaluate line-by-line, you'll be in debug mode anyway and sending to debug console as in 1 above. The Windows issue might have something to do with the settings.json entry
"terminal.integrated.inheritEnv": true,
not having an affect in Windows as of yet, per vscode documentation.

- 31
- 6
One way you can do it is through the Integrated Terminal. Here is the guide to open/use it: https://code.visualstudio.com/docs/editor/integrated-terminal
After that, type python3
or python
since it is depending on what version you are using. Then, copy and paste the fraction of code you want to run into the terminal. It now has the same functionality as the console in Spyder. Hope this helps.

- 695
- 1
- 6
- 21
Open up the Keyboard Shortcuts by hitting CTRL + Shift + P then type "Keyboard Shortcuts" and select "Preferences: Open Keyboard Shortcuts"
Then search for "Jupyter: Run From Line in Interactive Window"
It will most likely not be setup and have a little + sign next to it. Click that + sign, enter your preferred keyboard shortcut then hit "ENTER" and it will save. Now when you are coding on the left, you can hit this shortcut and it will run the code from that line in the Interactive Window.

- 21
- 1
- 8