Standard Input with Timeout
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
- gotcha Unlike the built-in `input()` function, `inputimeout()` raises a `TimeoutOccurred` exception if the timeout is reached. This exception *must* be explicitly caught.
- gotcha The behavior of `inputimeout` can be inconsistent in non-interactive environments (e.g., some IDEs like PyCharm's run window, CI/CD pipelines, or certain terminal emulators) or when `sys.stdin` is redirected. It might immediately raise `TimeoutOccurred` or other I/O errors.
Install
-
pip install inputimeout
Imports
- inputimeout
from inputimeout import inputimeout
- TimeoutOccurred
from inputimeout import TimeoutOccurred
Quickstart
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}")