Sensei Zaid
Sensei Zaid

Reputation: 363

VIM: EOFError: EOF when reading a line input python

I'm trying to add input to my python code

isAge = input("Enter your age: ")

and it shows me the following error:

enter your age: Traceback (most recent call last):
File "test2.py", line 3, in <module>
isAge = input("enter your age: ") 
EOFError: EOF when reading a line

I use "w !python" to run the code. I tried w !python 18" Where 18 is the age number, but it didnt work

Upvotes: 2

Views: 1249

Answers (1)

filbranden
filbranden

Reputation: 8898

If you use :w !python from Vim, it will start a Python interpreter and pipe the script to the interpreter, which means the Python interpreter's standard input will be connected to this pipe through which Vim will send it the script, and not to the console.

So Python functions such as input() will not work to read input from the user, since the interpreter does not have its standard input connected to the console.

To solve this, instead of having Vim pipe the script to a Python interpreter, have it invoke the Python interpreter with the script as an argument. So instead of :w !python, use:

:w
:!python %

This assumes you're editing a file and not an unnamed buffer. If you have an unnamed buffer, then save it to a *.py first.

Upvotes: 4

Related Questions