cbrnr
cbrnr

Reputation: 1751

Use IPython REPL in VS Code

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 Shift+Enter). However, this sends the selected code to a plain old Python REPL in the Terminal pane, whereas I'd like to have this code run in IPython instead (not the QtConsole, just the terminal-based IPython).

Is it possible to set IPython as the default REPL? I tried setting /usr/local/bin/ipython3 as my default Python environment, but that doesn't work (it still executes the plain Python interpreter). FWIW, I'm on macOS.

Upvotes: 62

Views: 55361

Answers (7)

Ryan Feeley
Ryan Feeley

Reputation: 667

If you have a default vanilla installation of the Python extension in VSCode, by default you can highlight python code and do "SHIFT+ENTER" to "Run Selection/Line in Python Terminal".

That command will use the default python.exe interpreter. However, this is the trick that works for me to use the IPython shell instead.

  1. First run a dummy line of python code by highlighting it and doing SHIFT+ENTER. This launches a terminal named "python" and starts the python shell to run the code in the REPL.
  2. Now issue exit() in that python shell to return to the regular terminal prompt.
  3. Run ipython in that terminal to start the IPython REPL where the plain old Python REPL used to be.

Now subsequent uses of SHIFT+ENTER (single or multiple lines highlighted) will run the code in the IPython shell.

(Note, if SHIFT+ENTER is sending code to the Python Interactive split window instead of a terminal REPL, make sure your settings.json has "jupyter.sendSelectionToInteractiveWindow": false,)

[EDIT]. Various comments on this thread remark that using the various solutions, code is copied to the IPython terminal, but not run. I realized I also have this experience depending on my active conda environment. If I do conda install -c conda-forge prompt-toolkit in the environment I'm using, I get the expected behavior where SHIFT+ENTER actually runs code. I don't know if that package is the key, one of its dependencies, or just using conda-forge. But it works!

Upvotes: 2

Feng Mai
Feng Mai

Reputation: 3109

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

Upvotes: 57

EEV
EEV

Reputation: 93

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.

Upvotes: 3

alex li
alex li

Reputation: 724

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

Upvotes: 10

uhbif19
uhbif19

Reputation: 3245

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.

Upvotes: 2

Ahmed Fasih
Ahmed Fasih

Reputation: 6917

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

import IPython
IPython.embed()

See IPython docs.

Upvotes: 8

Natsfan
Natsfan

Reputation: 4802

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.

Upvotes: 24

Related Questions