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 consoleMark– Mark2018-04-11 13:44:23 +00:00Commented 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.questionto42– questionto422020-07-28 06:04:47 +00:00Commented 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?starball– starball ♦2023-08-31 18:25:16 +00:00Commented Aug 31, 2023 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.
2 Comments
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
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
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
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.
