{"id":1514,"library":"inputimeout","title":"Standard Input with Timeout","description":"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.","status":"active","version":"1.0.4","language":"python","source_language":"en","source_url":"https://github.com/johejo/inputimeout","tags":["input","timeout","cli","utility","cross-platform"],"install":[{"cmd":"pip install inputimeout","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"inputimeout","correct":"from inputimeout import inputimeout"},{"symbol":"TimeoutOccurred","correct":"from inputimeout import TimeoutOccurred"}],"quickstart":{"code":"from inputimeout import inputimeout, TimeoutOccurred\n\ntry:\n    # Prompt for input with a 5-second timeout\n    user_input = inputimeout(prompt='Enter something within 5 seconds: ', timeout=5)\n    print(f\"You entered: {user_input}\")\nexcept TimeoutOccurred:\n    print(\"No input received within the time limit.\")\n    user_input = None\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")","lang":"python","description":"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."},"warnings":[{"fix":"Always wrap `inputimeout()` calls in a `try...except TimeoutOccurred` block to handle the case where no input is provided.","message":"Unlike the built-in `input()` function, `inputimeout()` raises a `TimeoutOccurred` exception if the timeout is reached. This exception *must* be explicitly caught.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"search_vec":"'1.0.4':44 'cli':57 'cross':10,60 'cross-platform':9,59 'current':40 'focus':53 'function':19 'infrequ':46 'input':2,18,32,55 'inputimeout':5 'interact':28 'librari':13 'lightweight':8 'limit':37 'matur':52 'platform':11,61 'provid':15 'python':12 'requir':34 'script':29 'specifi':22 'stabl':41 'standard':1,17 'timefram':38 'timeout':4,23,56 'updat':47 'use':26 'user':31 'util':54,58 'version':42 'within':35","created_at":"2026-04-09T03:51:12.588619+00:00","updated_at":"2026-04-16T15:45:08.125901+00:00","problems":[{"fix":"Install the library using pip: `pip install inputimeout`","cause":"The `inputimeout` library is not installed in your Python environment or the Python interpreter cannot find it.","error":"ModuleNotFoundError: No module named 'inputimeout'"},{"fix":"Ensure you import the function before using it: `from inputimeout import inputimeout, TimeoutOccurred`","cause":"The `inputimeout` function was called without being properly imported from the library.","error":"NameError: name 'inputimeout' is not defined"},{"fix":"Catch `TimeoutOccurred` specifically to differentiate between a timeout and other exceptions: `try: result = inputimeout(prompt='Enter:', timeout=5) except TimeoutOccurred: result = 'Timeout!'`","cause":"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.","error":"Inputimeout doesn't wait for user input and immediately sends timeout message / except Exception: catches TimeoutOccurred prematurely"},{"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.","cause":"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.","error":"OSError: [WinError 6] The handle is invalid"},{"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'`","cause":"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.","error":"NameError: name 'user_input' is not defined (or similar for a variable meant to store input)"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"1.0.4","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"http://github.com/johejo/inutimeout","docs":null,"changelog":null,"pypi":"https://pypi.org/project/inputimeout/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["http-networking","data"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-27","next_check":"2026-07-28","install_tag":"verified"}}