wakepy - wakelock / keep-awake / stay-awake

raw JSON →
1.0.0 verified Mon Apr 27 auth: no python

wakepy is a cross-platform library for keeping the system awake (preventing sleep / screen lock) and releasing the wakelock. Version 1.0.0 (requires Python >=3.7). Development is active on GitHub. It supports Windows, macOS, and Linux via DBus.

pip install wakepy
error ModuleNotFoundError: No module named 'wakepy'
cause wakepy not installed.
fix
pip install wakepy
error ImportError: cannot import name 'prevent_sleep' from 'wakepy'
cause API changed in version 1.0.0; prevent_sleep removed.
fix
Use 'from wakepy import keepawake' and 'with keepawake():' instead.
gotcha On Linux, wakepy requires D-Bus (typically via systemd or the freedesktop.org inhibitor interface). If DBus is not available, the context manager will raise an error or silently fail in older versions.
fix Ensure dbus service is running; use 'pip install wakepy[dbus]' for additional dependencies if needed (though not needed in 1.0.0).
gotcha keepawake() may require superuser/administrator privileges on some Linux distributions to inhibit sleep. Without proper permissions, the inhibit may be ignored.
fix Run Python with appropriate privileges or configure polkit rules for your user.
breaking In version 1.0.0, the API was simplified. The old import 'from wakepy import prevent_sleep' is removed. Use 'from wakepy import keepawake'.
fix Replace 'prevent_sleep' with 'keepawake'.

Basic usage as a context manager to prevent sleep.

from wakepy import keepawake

with keepawake():
    # Your long-running task here
    import time
    time.sleep(10)
    print("System stayed awake during this block.")