简体   繁体   English

VS Code 在终端中对 IPython shell 执行选择(无笔记本)

[英]VS Code execute selection to IPython shell in terminal (no notebook)

I wanted to use IPython interactive in VSC (not using a notebook).我想在 VSC 中使用 IPython 交互(不使用笔记本)。 By using a keybiding for workbench.action.terminal.runSelectedText, I can successfully execute a selection to a python interactive shell, but with iPython it fails to run the cell and I have to change selection to the terminal and press enter.通过对 workbench.action.terminal.runSelectedText 使用 keybiding,我可以成功地执行对 python 交互式 shell 的选择,但是使用 iPython 它无法运行单元格,我必须将选择更改为终端并按 Enter。

在此处输入图像描述

Here's my keybidings.json file:这是我的 keybidings.json 文件:

[
    {
        "key": "ctrl+enter",
        "command": "workbench.action.terminal.runSelectedText"
    }
]

Any help to solve this is much appreciated, Ideally.非常感谢任何帮助解决这个问题,理想情况下。 a keybinding configuration that includes an extra enter.包含额外输入的键绑定配置。

IPython is not officially supported by the Python extension and this is part of the reason: IPython's design simply requires you to press Enter an extra time based on how VS Code sends text into the terminal. Python 扩展不正式支持 IPython,这也是部分原因:IPython 的设计只是要求您根据 VS Code 将文本发送到终端的方式多按一次 Enter。

This is an issue with IPython's autoindent feature.这是 IPython 的自动缩进功能的问题。 You can disable this by passing --no-autoindent when launching IPython (ie ipython --no-autoindent ).您可以通过在启动 IPython 时传递--no-autoindent来禁用它(即ipython --no-autoindent )。

edit: added gif.编辑:添加 gif。

在此处输入图像描述

I was annoyed by this quirk in vs-code too.我也对 vs-code 中的这个怪癖感到恼火。

I noticed that when using a python virtual environment (I use pipenv ) my selected line was executed in an Ipython terminal and no extra Enter key press was needed.我注意到,当使用 python 虚拟环境(我使用pipenv )时,我选择的行是在Ipython终端中执行的,不需要额外的Enter按键。

To reproduce or test out:重现或测试:

  1. Launch VS Code启动 VS 代码
  2. Open a python file and a terminal window打开 python 文件和终端 window
  3. In the terminal run pipenv shell (requires pip install pipenv && pipenv install ipython )在终端运行pipenv shell (需要pip install pipenv && pipenv install ipython
  4. Then in the terminal run ipython (or ipython --no-autoindent )然后在终端运行ipython (或ipython --no-autoindent
  5. Select a line in your python file and from a vs-code command palette execute Terminal: Run Selected Text in Active Terminal (or use a keyboard shortcut) Select python 文件中的一行并从 vs-code 命令面板执行Terminal: Run Selected Text in Active Terminal (或使用键盘快捷键)

screenshot of python window and terminal window python window 和终端 window 的屏幕截图

I have this problem when I upgraded IPython to version 7.30.1 The solution I found is that add "--simple-prompt" , this is not perfect (just no color theme or auto completion), but at very least, you don't need to press enter when sending codes to run.当我将 IPython 升级到版本 7.30.1 时我遇到了这个问题我找到的解决方案是添加"--simple-prompt" ,这并不完美(只是没有颜色主题或自动完成),但至少,你没有发送代码运行时无需回车。

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

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