Use IPython REPL in VS Code

I start IPython from inside the standard Python REPL that's spawned by Shift-Enter with

import IPython
IPython.embed()

See IPython docs.


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.


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",
],

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.

Shortcut setting - Normal

  1. open shortcut setting: Macos it's cmd+k cmd+s.

  2. search command above and right click to modify the keyboard binding as shift+enter.

  3. Next, right click again to modify the When expression as:

editorTextFocus && !findInputFocussed && !python.datascience.ownsSelection && !replaceInputFocussed && editorLangId == 'python'
  1. Right click and select show same key bindings

  2. Find command Python: Run Selection/Line in Python Terminal and Right click to disable it.

Shortcut setting - JSON

  1. Open shortcut setting and click Upper right corner to open JSON config

  2. 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'"
    }