How can I prevent Windows 10 from any kind of sleep or hibernate when running python script?

Use the tool Don't Sleep while running your python script:

Don't Sleep is a small portable program to prevent system shutdown, Standby, Hibernate, Turn Off and Restart.

enter image description here

This prevents all action that can interrupt your script.


Runs a program preventing sleeping or the display turning off while the program runs. Doesn't affect the screensaver.

Using pywin32 library you can do exactly the same. I note that pywin32 doesn't actually wrap this function.

For more info see docs https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setthreadexecutionstate

NOTE: Do not use the constant ES_USER_PRESENT as it will always fail.

From above link

ES_AWAYMODE_REQUIRED 0x00000040 Enables away mode. This value must be specified with ES_CONTINUOUS. Away mode should be used only by media-recording and media-distribution applications that must perform critical background processing on desktop computers while the computer appears to be sleeping. See Remarks.

ES_CONTINUOUS 0x80000000 Informs the system that the state being set should remain in effect until the next call that uses ES_CONTINUOUS and one of the other state flags is cleared.

ES_DISPLAY_REQUIRED 0x00000002 Forces the display to be on by resetting the display idle timer.

ES_SYSTEM_REQUIRED 0x00000001 Forces the system to be in the working state by resetting the system idle timer.

To Use

KeepDisplayOn <commandline of program to run>
KeepSystemOn <commandline of program to run>

@Echo Off
ECHO Three files follow
ECHO PreventSleep.bat
ECHO.
ECHO This file compiles KeepDisplayOn.vb and KeepSystemOn.vb to KeepDisplayOn.exe and KeepSystemOn.exe using the system VB.NET compiler.
ECHO.
ECHO Runs a program preventing sleeping or the display turning off while the program runs
ECHO.
ECHO To Use
ECHO      KeepDisplayOn ^<commandline of program to run^>
ECHO      KeepSystemOn ^<commandline of program to run^>
ECHO.
C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc "%~dp0\KeepDisplayOn.vb" /out:"%~dp0\KeepDisplayOn.exe" /target:winexe
C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc "%~dp0\KeepSystemOn.vb" /out:"%~dp0\KeepSystemOn.exe" /target:winexe
pause

'KeepSystemOn.vb
imports System.Runtime.InteropServices 

Public Module MyApplication  

Public Declare UNICODE Function SetThreadExecutionState Lib "Kernel32" (ByVal esFlags as Integer) as Integer
Public Const  ES_AWAYMODE_REQUIRED = &h40
Public Const  ES_CONTINUOUS = &h80000000
Public Const  ES_DISPLAY_REQUIRED = &h2
Public Const  ES_SYSTEM_REQUIRED = &h1
Public Const  ES_USER_PRESENT = &h4


    Public Sub Main ()
        Dim wshshell as Object
        Dim Ret as Integer
        WshShell = CreateObject("WScript.Shell")
        Ret = SetThreadExecutionState(ES_Continuous + ES_System_Required + ES_Awaymode_Required)
        WshShell.Run(Command(), , True)
    End Sub 
End Module 

'KeepDisplayOn.vb
imports System.Runtime.InteropServices 

Public Module MyApplication  

Public Declare UNICODE Function SetThreadExecutionState Lib "Kernel32" (ByVal esFlags as Integer) as Integer
Public Const  ES_AWAYMODE_REQUIRED = &h40
Public Const  ES_CONTINUOUS = &h80000000
Public Const  ES_DISPLAY_REQUIRED = &h2
Public Const  ES_SYSTEM_REQUIRED = &h1
Public Const  ES_USER_PRESENT = &h4


    Public Sub Main ()
        Dim wshshell as Object
        Dim Ret as Integer
        WshShell = CreateObject("WScript.Shell")
        Ret = SetThreadExecutionState(ES_Continuous + ES_Display_Required + ES_Awaymode_Required)
        WshShell.Run(Command(), , True)
    End Sub 
End Module 

Posted to my repository as well https://winsourcecode.blogspot.com/2020/05/keepdisplayon-runs-program-preventing.html