Using input () in a subprocess in IPython started with %% script py -2

The following code uses the cell magic property to run Python 2 in a subprocess in IPython:

%%script py -2
print 'foo'

      

It works as expected (ie prints "foo").

But when I try to get user input in the subprocess, it fails:

%%script py -2
input('foo? ')

      

Here's the output:

foo? Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
EOFError: EOF when reading a line

      

Any ideas why the input won't work in the subprocess?

In case it helps, here's a screen capture:

IPython Python 2 in subprocess: input () fails

+3


source to share


1 answer


I'm guessing there is something odd s stdin

in the subprocess (it seems to be closed), so this might work around this:

>>> %%script python
... stdin = open('/dev/tty')
... line = stdin.readline()
... print 'input:', line  # python 2.7.6 is default on my sys
... 
yay!
input: yay!

      



The above result when running on IPython 1.2.1 with Python 3.4.0 (with% doctest_mode).

0


source







All Articles