如何在bpython中剪切和粘贴?

问题描述

如果可能,我想直接在 shell 会话中剪切和粘贴。我在 .py 文件中有一个简单的 with 块:

with open('t1_equip.json') as json_file:
    data = json.load(json_file)
    # Print the type of data variable
    print("Type:",type(data))

这将很容易地剪切并粘贴到解释器中。但是,如果我尝试直接在 bpython 会话中剪切和粘贴,则会引发错误

>>> with open('t1_equip.json') as json_file:
... ...     data = json.load(json_file)
  File "<bpython-input-57>",line 2
    ...     data = json.load(json_file)
    ^
IndentationError: expected an indented block
>>> ...     # Print the type of data variable
Ellipsis
>>> ...     print("Type:",type(data))
  File "<input>",line 1
    ...     print("Type:",type(data))
            ^
SyntaxError: invalid Syntax

我觉得必须有一个简单的工作流程更改,我只需按几个键就可以做到这一点。必须不断点击向上箭头才能重新插入可以做到,但是我们可以为整个最后一个块做 %hist 之类的事情或类似的事情吗?我如何使这成为可能?我将 zsh 与 oh-my-zsh 和 powerlevel10k 结合使用。

解决方法

不要使用 python 命令。使用ipython。您可能需要通过 pip install ipython 安装它。然后使用魔法命令 %paste。这将很容易复制引号块。

pip install ipython  #Install

ipython # Opens ipython prompt
%paste