PromptHub
Python Beginner 11 views

Python EOFError

The input() function encountered end-of-file (no more data to read).

Explanation

EOFError is raised when the input() function or similar input-reading functions reach the end of the input stream without reading any data. This commonly happens when running scripts in non-interactive environments (like cron jobs, Docker containers, or CI/CD pipelines) that try to read user input. It also occurs when reading from files that have been unexpectedly closed or when piped input is exhausted.

Common Causes

  • Running interactive script in non-interactive environment
  • stdin not available (Docker, cron, CI)
  • Input stream exhausted
  • File closed unexpectedly
  • Piped input ended

Solution

Remove input() calls from scripts that run in non-interactive environments. Use command-line arguments (argparse) instead of interactive input. Set default values for environments where input isn't available. Wrap input() calls in try-except blocks. For testing, provide input via stdin piping: echo "input" | python script.py. Use environment variables for configuration instead of interactive prompts.

Code Example

# This fails in non-interactive environments
name = input('Enter your name: ')  # EOFError in cron/Docker

# Fix: use default value for non-interactive
def get_name():
    if sys.stdin.isatty():
        return input('Enter your name: ')
    return os.environ.get('NAME', 'default')

# Or use try-except
try:
    name = input('Enter your name: ')
except EOFError:
    name = 'default'

# Better: use argparse for CLI tools
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--name', default='default')
args = parser.parse_args()
name = args.name

# For reading files
try:
    line = file.readline()
    if not line:
        print('End of file')
except EOFError:
    print('Unexpected end of file')

Error Information

Language

python

Difficulty

Beginner

Views

11

Related Errors