【问题标题】:Python in VS Code: Can I run cell in the integrated terminal?VS Code 中的 Python:我可以在集成终端中运行单元格吗?
【发布时间】:2020-04-17 15:46:08
【问题描述】:

在带有 Python 的 VS Code 中,我们可以在“Python 交互窗口”中运行“单元格”(以 #%% 开头的块)

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

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

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

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

【问题讨论】:

    标签: python visual-studio-code spyder vscode-settings


    【解决方案1】:

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

    我找到了另一种解决方法,类似于Diogo's answer,虽然涉及的程度略低,只需要一个额外的扩展(假设已经安装了 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"
        }
    

    【讨论】:

      【解决方案2】:

      如果您希望在 CURRENTLY ACTIVE 终端中执行单元格内容,您需要调整 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"
         }
      
      

      【讨论】:

        【解决方案3】:

        我开了一个issue in GitHub@AdamAL also opened,但似乎他们并没有这样做的意图。这是其他用户的解决方法。

        编辑:

        我之前回答过一个解决方法,它需要 2 个 VSCode 扩展而不使用智能命令 jupyter.selectCellContents
        @AdamAL 使用此命令共享了一个更好的解决方案,但需要注意的是光标会丢失位置和仅关注 Python 终端。
        @Maxime Beau 指出将 @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"
        },
        

        【讨论】:

          猜你喜欢
          • 2020-01-07
          • 2019-05-25
          • 2023-01-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-06-29
          • 1970-01-01
          • 2022-10-03
          相关资源
          最近更新 更多