Prevent OS X from going to sleep with Python?
Question: Is there a way to prevent a computer running OS X from going to sleep from
within a Python script?


Answer: You can call the **caffeinate** command.

    
    
    subprocess.Popen('caffeinate')
    

This is how I use it:

    
    
    import sys
    import subprocess
    
    if 'darwin' in sys.platform:
        print('Running \'caffeinate\' on MacOSX to prevent the system from sleeping')
        subprocess.Popen('caffeinate')
    

