0

Say I have a simple script that depends on my input:

w = input()
print(f'Input is {w}')

If I copy and paste this script (both lines at the same time) into the interactive window, it won't pause on input line to receive an input.

>>> w = input()
print(f'Input is {w}')
>>>

Is there any way to change this behavior?

Update: This seems to work just fine on Pycharm:

In: w = input()
print(f'Input is {w}')
>? test
Input is test
Yar
  • 7,020
  • 11
  • 49
  • 69
  • 2
    Why do you need to paste this into the interactive window in the first place? Why not just write it into a .py file and execute the file? – AKX Aug 03 '22 at 20:07
  • I just want to select the lines I want to test and run those instead of executing the whole file. – Yar Aug 03 '22 at 20:14
  • 1
    Well, in that case as a hacky workaround you can wrap them in an `if True:` block - the REPL won't evaluate an indented block until it's over (plus one newline). – AKX Aug 03 '22 at 20:15
  • Ah interesting! Thank you! – Yar Aug 03 '22 at 20:18
  • Why not paste one line at a time? Python's interactive mode is generally meant to be used one statement at a time. – wjandrea Aug 03 '22 at 20:42
  • 1
    right that is one solution, though that's a bit inconvenient to do it with multiple inputs in your code. – Yar Aug 03 '22 at 20:50

1 Answers1

1

You could use IPython, which supports pasting blocks:

In [1]: w = input()
   ...: print(f'Input is {w}')
a
Input is a

Just in case that doesn't work, you could use the command %paste to load and execute the clipboard contents, or %cpaste so that you can paste manually:

In [2]: %paste
w = input()
print(f'Input is {w}')

## -- End pasted text --
b
Input is b

In [4]: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:w = input()
:print(f'Input is {w}')
:--
ERROR! Session/line number was not unique in database. History logging moved to new session 1903
c
Input is c

(I'm not sure what this error means BTW, though I notice the "In" number ticked up once more than it should have.)

See also: Paste Multi-line Snippets into IPython

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • Tested on IPython and it works! I added `"python.terminal.launchArgs": ["-m", "IPython", "--no-autoindent"]` to my VSCode setting. – Yar Aug 04 '22 at 00:03