You could do something like this:
if __name__ == "__main__":
when_to_run = # Set first run datetime
time_to_wait = when_to_run - datetime.now()
while True:
time.sleep(time_to_wait.seconds)
# run your stuff here
when_to_run = # Set next run datetime
time_to_wait = when_to_run - datetime.now()
Say you want this to run every day at 10 AM, you set when_to_run to today at 10 AM or if that's already in the past, tomorrow at 10 AM, and add a day with timedelta in the loop. If you just set to sleep for 24 hours, the time of execution will get delayed by the time it took to execute it each time.
Example:
Run stuff every day at 1 PM:
if __name__ == "__main__":
when_to_run = datetime.now().replace(hour=13, minute=0, second=0, microsecond=0)
if datetime.now() > when_to_run:
# First run is tomorrow
when_to_run += timedelta(days=1)
time_to_wait = when_to_run - datetime.now()
while True:
time.sleep(time_to_wait.seconds)
# run your stuff here
stuff.run()
when_to_run += timedelta(days=1)
time_to_wait = when_to_run - datetime.now()