Python can't read script from stdin when input() appears in the script.
by mimorek from LinuxQuestions.org on (#5GZJP)
Hello,
When I let python run a short script from stdin it works, but not when the input() function appears in the script.
What is behind the EOFError and is there a way to make this work?
Thanks.
This fails:
Code:$ python3 <<HERE
> x = input()
> x = int(x)
> print(x**3)
> HERE
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
EOFError: EOF when reading a line
This works:
Code:$ python3 <<HERE
> x = 3**3
> print(x)
> HERE
27


When I let python run a short script from stdin it works, but not when the input() function appears in the script.
What is behind the EOFError and is there a way to make this work?
Thanks.
This fails:
Code:$ python3 <<HERE
> x = input()
> x = int(x)
> print(x**3)
> HERE
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
EOFError: EOF when reading a line
This works:
Code:$ python3 <<HERE
> x = 3**3
> print(x)
> HERE
27