简体   繁体   English

在 VS Code 中使用 IPython REPL

[英]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).使用 Visual Studio Code 的 Python 扩展,我可以选择一些代码,右键单击它,然后选择“在 Python 终端中运行选择/行”(或者,我可以按 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).但是,这会将所选代码发送到终端窗格中的普通旧 Python REPL,而我希望此代码在 IPython 中运行(不是 QtConsole,只是基于终端的 IPython)。

Is it possible to set IPython as the default REPL?是否可以将 IPython 设置为默认 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).我尝试将/usr/local/bin/ipython3为我的默认 Python 环境,但这不起作用(它仍然执行普通的 Python 解释器)。 FWIW, I'm on macOS. FWIW,我在 macOS 上。

Type Ipython inside the terminal window.在终端窗口中输入 Ipython。 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.然后从编辑器窗口中选择要运行的一行或多行,然后单击 VScode 窗口顶部的终端菜单。 One option in the Terminal menu is to "Run Selected Text".终端菜单中的一个选项是“运行选定的文本”。 This will be run in the Ipython terminal window.这将在 Ipython 终端窗口中运行。 I don't know how to make this the default but it appears to remain in that state unless Ipython is stopped.我不知道如何将其设为默认值,但除非停止 Ipython,否则它似乎保持该状态。 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.在编辑器窗口中右键单击并单击“运行选择”将不会使用 Ipython 窗口。 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.添加以下设置(首选项:打开设置 JSON;或首选项 -> 设置 -> 搜索launchArgs -> 在 json 中编辑)无需任何扩展即可工作。 It also fixes the issue that multiple lines cannot be sent to Python.它还修复了无法将多行发送到 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:更新 (2020-12-27):以下设置似乎效果更好,因为它支持 Ctrl+C 键盘中断,而无需现有 IPython:

"python.terminal.launchArgs": [
    "-m",
    "IPython",
    "--no-autoindent",
],

Use "IPython for VSCode" plugin.使用“IPython for VSCode”插件。

Install it and then use Send Select Text (or current line) To IPython安装它,然后使用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.如果你想与原来使用的快捷键设置shift+enter执行上面的命令,下面的方法使用一个

Shortcut setting - Normal快捷方式设置 - 正常

  1. open shortcut setting: Macos it's cmd+k cmd+s .打开快捷方式设置:Macos 它是cmd+k cmd+s

  2. search command above and right click to modify the keyboard binding as shift+enter .上面的搜索命令并右键单击以将键盘绑定修改为shift+enter

  3. Next, right click again to modify the When expression as:接下来,再次右键单击以将When expression修改为:

editorTextFocus && !findInputFocussed && !python.datascience.ownsSelection && !replaceInputFocussed && editorLangId == 'python'
  1. Right click and select show same key bindings右键单击并选择show same key bindings

  2. Find command Python: Run Selection/Line in Python Terminal and Right click to disable it.查找命令Python: Run Selection/Line in Python Terminal并右键单击以禁用它。

Shortcut setting - JSON快捷方式设置 - JSON

  1. Open shortcut setting and click Upper right corner to open JSON config打开快捷方式设置,点击右上角打开JSON配置

  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我从 Shift-Enter 生成的标准 Python REPL内部启动 IPython

import IPython
IPython.embed()

See IPython docs .请参阅IPython 文档

You could also set the "python.pythonPath" in your settings.json as follows:您还可以在settings.json "python.pythonPath" ,如下所示:

{
  "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. shift+enter将触发 ipython 并将线路发送到终端。

IPython support is provided by "IPython for VSCode" plugin. IPython 支持由“IPython for VSCode”插件提供。

Just select the text and invoke 'Send Selected Text (or current line) To IPython' in command palette.只需选择文本并在命令面板中调用“将所选文本(或当前行)发送到 IPython”。

Also official Microsoft Python plugin now supports interactive Jupiter windows, with similar functionality.此外,官方的 Microsoft Python 插件现在支持具有类似功能的交互式 Jupiter 窗口。

How to create a Jupiter Notebook in VS Code如何在 VS Code 中创建 Jupiter Notebook

  1. Go to the command palette (Command + Shift + P)转到命令面板(Command + Shift + P)
  2. Search for: "Jupyter: Create New Blank Notebook", and hit enter搜索:“Jupyter:创建新的空白笔记本”,然后按 Enter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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