简体   繁体   中英

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.

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

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

import IPython
IPython.embed()

See IPython docs .

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.

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.

How to create a Jupiter Notebook in VS Code

  1. Go to the command palette (Command + Shift + P)
  2. Search for: "Jupyter: Create New Blank Notebook", and hit enter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM