26

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.

3
  • Possible duplicate of VS Code execute current line or selection to in the integrated console Commented Apr 11, 2018 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. Commented Jul 28, 2020 at 6: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? Commented Aug 31, 2023 at 18:25

6 Answers 6

9

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
You want the Interactive Windows which has a variable explorer. Then you can run code line-by-line.
5

You can:

  1. open a terminal at Terminal>New Terminal
  2. Highlight the code you want to run
  3. 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.

enter image description here

Comments

4

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 Comments

This is the actual answer of today‘s versions.
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.
2

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.

Comments

1

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.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.