Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VS代码中的Python:我可以在集成终端中运行cell吗?_Python_Visual Studio Code_Spyder_Vscode Settings - Fatal编程技术网

VS代码中的Python:我可以在集成终端中运行cell吗?

VS代码中的Python:我可以在集成终端中运行cell吗?,python,visual-studio-code,spyder,vscode-settings,Python,Visual Studio Code,Spyder,Vscode Settings,在使用Python编写的VS代码中,我们可以在“Python交互窗口”中运行一个“单元”(以#%%开头的块) 我们可以在集成终端上做同样的事情吗 我知道我们可以在Spyder中实现这一点,在Spyder中,终端通常都是IPython终端 Matlab的工作原理与它的终端相同 我们可以在VS代码中这样做吗?我已经打开了一个,但他们似乎没有这样做的意图。这里是其他用户的解决方法 编辑: 我之前已经用一种变通方法回答了这个问题,该方法需要2个VSCode扩展名,并且不使用smart命令jupyter。

在使用Python编写的VS代码中,我们可以在“Python交互窗口”中运行一个“单元”(以#%%开头的块)

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

我知道我们可以在Spyder中实现这一点,在Spyder中,终端通常都是IPython终端

Matlab的工作原理与它的终端相同

我们可以在VS代码中这样做吗?

我已经打开了一个,但他们似乎没有这样做的意图。这里是其他用户的解决方法

编辑: 我之前已经用一种变通方法回答了这个问题,该方法需要2个VSCode扩展名,并且不使用smart命令
jupyter。选择cellcontents

使用此命令共享了一个更好的解决方案,但需要注意的是,光标会失去位置,并且仅在Python终端中聚焦。
指出将@AdamAL解决方案扩展到活动终端(例如,可能是IPython)

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

1。安装扩展插件:

{
    "key": "ctrl+alt+enter",
    "command": "macros.runCellinTerminal",
    "when": "editorTextFocus && jupyter.hascodecells"
},
{
    "key": "shift+alt+enter",
    "command": "macros.runCellinTerminaladvance",
    "when": "editorTextFocus && jupyter.hascodecells"
},
此扩展有一些没有的附加命令(如
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终端时,
delay
sendSequence\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"
},
有一个问题。请也去投票吧@Diogo的功能请求已关闭

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

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

  • 安装

  • settings.json
    中:

  • “多命令.命令”:[
    {
    “命令”:“multicmand.runCellInPythonTerminal”,
    “标签”:“在python终端中运行单元格”,
    “顺序”:[
    “jupyter.selectCellContents”,
    “python.execSelectionTerminal”,
    “光标镇”
    ]
    }
    ]
    
  • keybinding.json
    中:
  • {
    “键”:“ctrl+shift+enter”,
    “命令”:“multicmand.runCellInPythonTerminal”,
    “当”:“editorTextFocus&&jupyter.hascodecells”
    }
    
    如果您希望在当前活动终端中执行单元格内容,则需要调整AdamAL的解决方案:

  • 安装多命令

  • 在:

  • 在keybinding.json(与settings.json位置相同)中:
  •    {
           "key": "ctrl+shift+enter",
           "command": "multiCommand.runCellInPythonTerminal",
           "when": "editorTextFocus && jupyter.hascodecells"
       }