The following code worked just fine on Kali Linux for the purpose of preventing sleep. The above answer that involved setting the volume up and down did not work on Kali. I do not know about Windows or MacOS.
import pyautogui, time
while True:
pyautogui.moveTo(100, 100)
pyautogui.moveTo(500, 500)
time.sleep(5)