Using the Python extension of Visual Studio Code, I can select some code, right-click it, and select \"Run Selection/Line in Python Terminal\" (alternatively, I can hit Shif
Adding the following setting (Preference: Open Settings JSON; or Preference -> Settings -> Search launchArgs
-> edit in json) works without any extension. It also fixes the issue that multiple lines cannot be sent to Python.
"python.terminal.launchArgs": [
"-c",
"\"import subprocess; subprocess.call(['ipython', '--no-autoindent'])\""
],
Update (2020-12-27): the following setting seems to work better because it supports Ctrl+C keyboard interrupt without existing IPython:
"python.terminal.launchArgs": [
"-m",
"IPython",
"--no-autoindent",
],
Type Ipython inside the terminal window. Then select the line or lines you want to run from the editor window and then click on the Terminal menu at the top of VScode window. One option in the Terminal menu is to "Run Selected Text". This will be run in the Ipython terminal window. I don't know how to make this the default but it appears to remain in that state unless Ipython is stopped. Note: You have to run your selections using the Menu item. Right-clicking in the editor window and clicking on "Run Selection" will not use the Ipython window. I hope this is clear. If not just drop a comment.
IPython support is provided by "IPython for VSCode" plugin.
Just select the text and invoke 'Send Selected Text (or current line) To IPython' in command palette.
Also official Microsoft Python plugin now supports interactive Jupiter windows, with similar functionality.
You could also set the "python.pythonPath"
in your settings.json
as follows:
{
"python.pythonPath": "~/miniconda3/bin/ipython3",
"python.dataScience.sendSelectionToInteractiveWindow": false
}
or
{
"python.pythonPath": "~/miniconda3/envs/<yourEnv>/bin/ipython3",
"python.dataScience.sendSelectionToInteractiveWindow": false
}
shift+enter
will then trigger ipython and send the line to the terminal.
Use "IPython for VSCode" plugin.
Install it and then use Send Select Text (or current line) To IPython
If you want use shortcut setting with original shift+enter
to execute command above, Use One of below methods.
open shortcut setting: Macos it's cmd+k cmd+s
.
search command above and right click to modify the keyboard binding as shift+enter
.
Next, right click again to modify the When expression
as:
editorTextFocus && !findInputFocussed && !python.datascience.ownsSelection && !replaceInputFocussed && editorLangId == 'python'
Right click and select show same key bindings
Find command Python: Run Selection/Line in Python Terminal
and Right click to disable it.
Open shortcut setting and click Upper right corner to open JSON config
Append these settings:
{
"key": "shift+enter",
"command": "ipython.sendSelectedToIPython",
"when": "editorTextFocus && !findInputFocussed && !python.datascience.ownsSelection && !replaceInputFocussed && editorLangId == 'python'"
},
{
"key": "shift+enter",
"command": "-python.execSelectionInTerminal",
"when": "editorTextFocus && !findInputFocussed && !python.datascience.ownsSelection && !replaceInputFocussed && editorLangId == 'python'"
}
I start IPython from inside the standard Python REPL that's spawned by Shift-Enter with
import IPython
IPython.embed()
See IPython docs.