VS Code execute selection to IPython shell in terminal (no notebook)

Viewed 1422

I wanted to use IPython interactive in VSC (not using a notebook). By using a keybiding for workbench.action.terminal.runSelectedText, I can successfully execute a selection to a python interactive shell, but with iPython it fails to run the cell and I have to change selection to the terminal and press enter.

enter image description here

Here's my keybidings.json file:

[
    {
        "key": "ctrl+enter",
        "command": "workbench.action.terminal.runSelectedText"
    }
]

Any help to solve this is much appreciated! Ideally, a keybinding configuration that includes an extra enter.

4 Answers

I have this problem when I upgraded IPython to version 7.30.1 The solution I found is that add "--simple-prompt", this is not perfect (just no color theme or auto completion), but at very least, you don't need to press enter when sending codes to run.

IPython is not officially supported by the Python extension and this is part of the reason: IPython's design simply requires you to press Enter an extra time based on how VS Code sends text into the terminal.

This is an issue with IPython's autoindent feature. You can disable this by passing --no-autoindent when launching IPython (i.e. ipython --no-autoindent).

edit: added gif.

enter image description here

I was annoyed by this quirk in vs-code too.

I noticed that when using a python virtual environment (I use pipenv) my selected line was executed in an Ipython terminal and no extra Enter key press was needed.

To reproduce or test out:

  1. Launch VS Code
  2. Open a python file and a terminal window
  3. In the terminal run pipenv shell (requires pip install pipenv && pipenv install ipython)
  4. Then in the terminal run ipython (or ipython --no-autoindent)
  5. Select a line in your python file and from a vs-code command palette execute Terminal: Run Selected Text in Active Terminal (or use a keyboard shortcut)

screenshot of python window and terminal window

Related