Standard Input with Timeout

1.0.4 · active · verified Thu Apr 09

inputimeout is a lightweight, cross-platform Python library that provides a standard input function with a specified timeout. It's useful for interactive scripts where user input is required within a limited timeframe. The current stable version is 1.0.4, with infrequent updates as it's a mature, focused utility.

Warnings

Install

Imports

Quickstart

This example demonstrates how to prompt a user for input with a 5-second timeout. If the user doesn't provide input within the specified time, a `TimeoutOccurred` exception is raised, which should be handled to prevent your program from crashing and to provide a fallback behavior.

from inputimeout import inputimeout, TimeoutOccurred

try:
    # Prompt for input with a 5-second timeout
    user_input = inputimeout(prompt='Enter something within 5 seconds: ', timeout=5)
    print(f"You entered: {user_input}")
except TimeoutOccurred:
    print("No input received within the time limit.")
    user_input = None
except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →