How to cut and paste in bpython?

141 Views Asked by At

I would like to cut and paste directly in the shell session, if possible. I have a simple with block in a .py file:

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

This will cut and paste easily into the interpreter. However if I try to cut and paste directly in the bpython session itself it throws errors:

>>> 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

I feel like there must be an easy workflow change I could make to do this in just a couple keystrokes. Having to continually hit the up arrow to reinsert can do it, but can we do something like a %hist for the entire last block or something similar? How do I make this possible? I'm using zsh with oh-my-zsh and powerlevel10k.

1

There are 1 best solutions below

0
On

Don't use the python command. Use ipython. You might need to install it via pip install ipython. Then use magic command, %paste. This will easily copy the block of quotes.

pip install ipython  #Install

ipython # Opens ipython prompt
%paste