{"id":7841,"library":"uptime","title":"Uptime Library","description":"The `uptime` library provides a cross-platform way to retrieve system uptime and boot time, aiming to do so without relying on external processes like parsing `uptime(1)` command output. It works on various major platforms, including Linux, Windows, macOS, and BSD systems. Despite its current version 3.0.1 being released in 2013, it remains a stable and widely used utility for basic system information.","status":"active","version":"3.0.1","language":"en","source_language":"en","source_url":"https://github.com/Cairnarvon/uptime","tags":["system","uptime","monitoring","cross-platform"],"install":[{"cmd":"pip install uptime","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"symbol":"uptime","correct":"from uptime import uptime"},{"symbol":"boottime","correct":"from uptime import boottime"}],"quickstart":{"code":"from uptime import uptime, boottime\nimport datetime\n\ndef run_quickstart():\n    try:\n        system_uptime_seconds = uptime()\n        if system_uptime_seconds is not None:\n            print(f\"System Uptime: {system_uptime_seconds:.2f} seconds\")\n            uptime_timedelta = datetime.timedelta(seconds=system_uptime_seconds)\n            print(f\"System Uptime (formatted): {uptime_timedelta}\")\n        else:\n            print(\"Could not determine system uptime.\")\n\n        system_boot_time = boottime()\n        if system_boot_time is not None:\n            print(f\"System Boot Time: {system_boot_time}\")\n        else:\n            print(\"Could not determine system boot time.\")\n\n    except RuntimeError as e:\n        print(f\"An error occurred: {e}\")\n    except Exception as e:\n        print(f\"An unexpected error occurred: {e}\")\n\nif __name__ == '__main__':\n    run_quickstart()","lang":"python","description":"This quickstart demonstrates how to fetch the system's uptime in seconds and its boot time as a `datetime` object. It includes error handling for cases where uptime or boot time cannot be determined."},"warnings":[{"fix":"Ensure your Python installation's `ctypes` module is functioning correctly. It is advisable to test `uptime` on your specific deployment environment before critical reliance. If `None` is returned, implement checks in your code.","message":"The `uptime` library heavily relies on Python's standard `ctypes` module for platform-specific system calls. On less common or custom Python installations, a broken or misconfigured `ctypes` can result in incorrect uptime values or `None` being returned.","severity":"gotcha","affected_versions":"All"},{"fix":"Always check the return type of each function. For calculations involving both, convert `float` uptime to a `datetime.timedelta` object, or the `datetime.datetime` to a timestamp, to prevent `TypeError`.","message":"`uptime.uptime()` returns the system's uptime as a `float` representing seconds, while `uptime.boottime()` returns the boot time as a `datetime.datetime` object. These different return types should be handled appropriately in arithmetic or display operations.","severity":"gotcha","affected_versions":"All (since 2.0 for `boottime` return type)"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Install the package using pip: `pip install uptime`.","cause":"The `uptime` package is not installed in the current Python environment.","error":"ModuleNotFoundError: No module named 'uptime'"},{"fix":"This indicates a potentially corrupted or non-standard Python installation. Verify the integrity of your Python installation and ensure the `datetime` module is present and importable. Reinstalling Python might be necessary.","cause":"The standard `datetime` module, which is a dependency for `uptime.boottime()`, is missing or inaccessible in the Python environment. This is highly unusual for a standard library module.","error":"RuntimeError: The datetime module isn't available"}]}