Use IPython REPL in VS Code

前端 未结 6 1382
天涯浪人
天涯浪人 2020-12-29 04:15

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 Shif

相关标签:
6条回答
  • 2020-12-29 04:42

    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",
    ],
    
    0 讨论(0)
  • 2020-12-29 04:49

    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.

    0 讨论(0)
  • 2020-12-29 04:49

    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.

    0 讨论(0)
  • 2020-12-29 05:00

    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.

    0 讨论(0)
  • 2020-12-29 05:02

    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'"
        }
    
    0 讨论(0)
  • 2020-12-29 05:05

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

    import IPython
    IPython.embed()
    

    See IPython docs.

    0 讨论(0)
提交回复
热议问题