62

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.

4

7 Answers 7

59

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",
],
Sign up to request clarification or add additional context in comments.

5 Comments

This works great, thank you! All I do when I want to run python code via ipython in vscode, is open the terminal in vscode and type ipython at the start of my session. Then shift + enter on my script sends it to ipython. If I didn't have this code snippet in my settings, it would add indents that were unnecessary. Thanks again!
Is there any way to make IPython run from the current file's working directory? If not, the solution below is still better with --no-autoident argument as you proposed.
Excellent answer, I think -i should be added to this list.
Yes, add -i to the arguments such that iPython doesn't exit allowing you to interact with the variables, etc. The order matters though. Make sure to add it after -m IPython such that the arg is passed to iPython, not python.
I was looking for a way to run bpython as the REPL and this worked like a charm, thanks! "python.terminal.launchArgs": ["-m", "bpython"]
24

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.

8 Comments

Nice, this works! I can't believe this is so hard to find, it seems like the Python plugin should default to this behavior instead of what it is doing right now. I've mapped this function to the Shift+Enter shortcut and it works perfectly!
Hm, unfortunately this doesn't quite work as expected. If I select multiple lines, only the first line of the selection runs in the terminal. Seems like a bug, I'll see if anyone has already reported it.
It worked for me with muliple lines. I don't understand why it didn't work for you.
I tried again with multiple lines and it did NOT work. My previous comment was wrong. I tried several things but I couldn't get it to run more than one line.
|
10

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

2 Comments

Thanks! Is there any way to automatically move the focus back to the editor after sending the text to IPython?
Hi, sorry for comment later. Most cases, I need the curser focus on ipython. If you want it focus back, maybe you can use shortcut Cmd + number depend on your Group.
8

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

import IPython
IPython.embed()

See IPython docs.

4 Comments

while this worked to add the code to an IPython instance, it still doesn't run the code, instead, pressing Shift + Enter only adds the code + "\n" (i.e. a next line).
Except for text coloring, this is most preferred one.
I use Ctrl+D to close the plain Python REPL spawned by Shift+Enter and than simply start the IPython REPL from the command line in the terminal window (I'm using WSL with VS Code), which gives me colours. Code sent from the editor is interpreted here.
@zero: If I understood it correctly, you need to select the code you want to run (possibly the whole script, at first), and use shift+enter again. You can then code line by line, and you see the results directly in IPython.
3

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.

1 Comment

This works, but it looks like this could mess up other things because the python.pythonPath setting might be needed in other places. I found that mapping Shift+Enter to "Terminal: Run Selected Text in Active Terminal" (workbench.action.terminal.runSelectedText) runs the selected lines in the active terminal (where I first manually start IPython). This now also works with multiple lines and indentation levels. It requires only remapping one keyboard shortcut.
2

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.

Comments

2

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!

4 Comments

Quick update. For IPython to actually execute the code, rather than just have it copied into the prompt, the package versions/sources seem to matter. I'm using Anaconda on Windows, and if I use the default base environment, then I have to press ENTER an extra time. However, if I use the conda-forge channel, I get an environment where IPython works without requiring extra ENTER presses. My theory is the "prompt_toolkit" is the key package, but perhaps it is one of its dependencies. You can get it from conda-forge with conda install -c conda-forge prompt-toolkit.
Unfortunately not work for me. i create new env for testing but failed. i still find no way to use ipython in vscode like spyder so far.Every comments about this can only copy to ipython but not run in ipython later.
@Anthony Maybe you can comment on the github issue related to this. Currently the VSCode devs appear to think IPython already works. github.com/microsoft/vscode-python/issues/…
This is a great approach. After exiting REPL, I can navigate to the specific folder where I'm running my script from, start IPython and off to the races I go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.