Python 在 VS Code 中:我可以在集成终端中 运行 单元格吗?

Python in VS Code: Can I run cell in the integrated terminal?

在带有 Python 的 VS Code 中,我们可以 运行 一个 "cell"(以 #%% 开头的块)在 "Python Interactive Window"

我们可以在综合终端上做同样的事情吗?

我知道我们可以在 Spyder 中执行此操作,其中终端通常总是 IPython 终端

Matlab 与其终端的工作方式相同。

我们可以在 VS Code 中做到这一点吗?

我开了一个issue in GitHub, @AdamAL also opened,但是他们好像没有这个打算。这是其他用户的解决方法。

编辑:

我之前回答过一个需要 2 VSCode 个扩展并且不使用智能命令 jupyter.selectCellContents.
的解决方法 使用此命令分享了一个更好的解决方案,但需要注意的是光标会丢失位置并仅在 Python 终端中聚焦。
指出将 @AdamAL 解决方案扩展到活动终端(例如 IPython)

现在我正在收集所有答案并发布通用解决方案。当 运行 只有一个单元格时,此解决方案不会丢失光标位置。还有另一个命令运行单元格并前进到下一个单元格(如在 Jupyter 中),它对活动终端是通用的(可能是 IPython)。

1.安装 macros 扩展:

此扩展有一些 multi-command 没有的额外命令(如 delay 命令)

2。在settings.json

"macros.list": {
    "runCellinTerminal": [
        "jupyter.selectCellContents",
        "workbench.action.terminal.runSelectedText",
        "cursorUndo",
        {"command": "$delay","args": {"delay": 100}},
        {"command": "workbench.action.terminal.sendSequence","args": { "text": "\n" }},
    ],
    "runCellinTerminaladvance": [
        "jupyter.selectCellContents",
        "workbench.action.terminal.runSelectedText",
        "cursorDown",
        {"command": "$delay","args": {"delay": 100}},
        {"command": "workbench.action.terminal.sendSequence","args": { "text": "\n" }},
    ],
}

OBS: cursorUndo 将光标移回正确位置。 cursorDown 将光标移动到下一个单元格。当终端是 IPython 终端时,命令 delaysendSequence \n 很有用。

3。在 keybinding.json:

{
    "key": "ctrl+alt+enter",
    "command": "macros.runCellinTerminal",
    "when": "editorTextFocus && jupyter.hascodecells"
},
{
    "key": "shift+alt+enter",
    "command": "macros.runCellinTerminaladvance",
    "when": "editorTextFocus && jupyter.hascodecells"
},

有一个new feature request for this here。请也去那里投票。 @Diogo 的功能请求已关闭。

我发现了另一种解决方法,类似于 ,但涉及的内容稍微少一些,只需要一个额外的扩展(假设已经安装了 MS 的 Python)。

需要注意的是您丢失了光标位置(执行后跳转到块的末尾)。

  1. 安装multi-command

  2. settings.json:

    "multiCommand.commands": [
        {
            "command": "multiCommand.runCellInPythonTerminal",
            "label": "Run cell in python terminal",
            "sequence": [
                "jupyter.selectCellContents",
                "python.execSelectionInTerminal",
                "cursorDown"
            ]
        }
    ]
  1. keybinding.json中:
    {
        "key": "ctrl+shift+enter",
        "command": "multiCommand.runCellInPythonTerminal",
        "when": "editorTextFocus && jupyter.hascodecells"
    }

如果您希望在当前活动的终端中执行单元格内容,您需要调整 AdamAL 的解决方案:

  1. 安装多命令

  2. settings.json:

    "multiCommand.commands": [
        {
            "command": "multiCommand.runCellInPythonTerminal",
            "label": "Run cell in python terminal",
            "sequence": [
                "jupyter.selectCellContents",
                "workbench.action.terminal.runSelectedText",
            ]
        }
    ]
  1. 在 keybinding.json(与 settings.json 相同的位置):
   {
       "key": "ctrl+shift+enter",
       "command": "multiCommand.runCellInPythonTerminal",
       "when": "editorTextFocus && jupyter.hascodecells"
   }