Registry / http-networking / inputimeout

inputimeout

JSON →
library1.0.4pypypi✓ verified 24d ago

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.

pip install inputimeout
INSTALL
IMPORT
SIG · INPUTIMEOUT
I
inputimeout
http-networkingpythonv1.0.4
Install
1.5s avg
Import
10ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0.4 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.004s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.002s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

inputimeout
from inputimeout import inputimeout
TimeoutOccurred
from inputimeout import TimeoutOccurred

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}")
Debug
Known issues
gotchaUnlike the built-in `input()` function, `inputimeout()` raises a `TimeoutOccurred` exception if the timeout is reached. This exception *must* be explicitly caught.
fix
Always wrap `inputimeout()` calls in a `try...except TimeoutOccurred` block to handle the case where no input is provided.
affects: All versions
gotchaThe 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.
fix
Test your application's input behavior thoroughly in the target environment. Consider providing a fallback mechanism (e.g., using command-line arguments or environment variables) for non-interactive scenarios, or checking `sys.stdin.isatty()` before attempting timed input.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'inputimeout'
The `inputimeout` library is not installed in your Python environment or the Python interpreter cannot find it.
fix
Install the library using pip: `pip install inputimeout`
NameError: name 'inputimeout' is not defined
The `inputimeout` function was called without being properly imported from the library.
fix
Ensure you import the function before using it: `from inputimeout import inputimeout, TimeoutOccurred`
Inputimeout doesn't wait for user input and immediately sends timeout message / except Exception: catches TimeoutOccurred prematurely
The code is using a general `except Exception:` block which catches the `TimeoutOccurred` exception (raised by `inputimeout`) before specific handling, leading to unexpected immediate timeout behavior.
fix
Catch `TimeoutOccurred` specifically to differentiate between a timeout and other exceptions: `try: result = inputimeout(prompt='Enter:', timeout=5) except TimeoutOccurred: result = 'Timeout!'`
OSError: [WinError 6] The handle is invalid
This error typically occurs on Windows systems when `inputimeout` (which relies on underlying I/O operations) is run in environments where standard input/output handles are not properly available or have been closed, such as a Windows Service, a GUI application without a console, or when compiled with PyInstaller using the `--noconsole` flag.
fix
Run the application from a console, ensure the environment has valid stdin/stdout handles, or consider not using `--noconsole` with PyInstaller if interactivity is required. For Python services, `inputimeout` is generally not suitable due to its reliance on console I/O. For subprocess calls, explicitly redirecting stdin to `subprocess.DEVNULL` might help prevent handle issues in some contexts, though this is less directly applicable to `inputimeout` itself.
NameError: name 'user_input' is not defined (or similar for a variable meant to store input)
The variable intended to store the user's input is only assigned within the `try` block, and if a `TimeoutOccurred` exception is raised, that assignment is skipped, leaving the variable undefined when accessed outside the `try` block.
fix
Initialize the variable with a default value before the `try` block or ensure it's assigned a value in both the `try` and `except` blocks: `user_input = None # Initialize before try block try: user_input = inputimeout(prompt='Your choice:', timeout=10) except TimeoutOccurred: user_input = 'default_value'`
Upgrade
Version history
1.0.4latest on PyPI · released Mar 2, 2018
Audit
Dependencies

No dependency data recorded yet.

Agent activity
13 hits · last 30 days
node
10
Resources