Python 3: EOF when reading a line (Sublime Text 2 is angry)
I had the same problem. The problem with the Sublime Text's default console is that it does not support input.
To solve it, you have to install a package called SublimeREPL. SublimeREPL provides a Python interpreter which accepts input.
There is an article that explains the solution in detail.
help(input)
shows what keyboard shortcuts produce EOF, namely, Unix: Ctrl-D, Windows: Ctrl-Z+Return:
input([prompt]) -> string
Read a string from standard input. The trailing newline is stripped. If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError. On Unix, GNU readline is used if enabled. The prompt string, if given, is printed without a trailing newline before reading.
You could reproduce it using an empty file:
$ touch empty$ python3 -c "input()" < emptyTraceback (most recent call last): File "<string>", line 1, in <module>EOFError: EOF when reading a line
You could use /dev/null
or nul
(Windows) as an empty file for reading. os.devnull
shows the name that is used by your OS:
$ python3 -c "import os; print(os.devnull)"/dev/null
Note: input()
happily accepts input from a file/pipe. You don't need stdin
to be connected to the terminal:
$ echo abc | python3 -c "print(input()[::-1])"cba
Either handle EOFError
in your code:
try: reply = input('Enter text')except EOFError: break
Or configure your editor to provide a non-empty input when it runs your script e.g., by using a customized command line if it allows it: python3 "%f" < input_file
EOF is a special out-of-band signal which means the end of input. It's not a character (though in the old DOS days, 0x1B acted like EOF), but rather a signal from the OS that the input has ended.
On Windows, you can "input" an EOF by pressing Ctrl+Z
at the command prompt. This signals the terminal to close the input stream, which presents an EOF to the running program. Note that on other OSes or terminal emulators, EOF is usually signalled using Ctrl+D
.
As for your issue with Sublime Text 2, it seems that stdin
is not connected to the terminal when running a program within Sublime, and so consequently programs start off connected to an empty file (probably nul
or /dev/null
). See also Python 3.1 and Sublime Text 2 error.