'Python sched module only works when the time is past otherwise it crashes

When I run this code

import sched,time,webbrowser

def action():
webbrowser.open("https://www.youtube.com/watch?v=dQw4w9WgXcQ")

a = time.strptime('Thu Jan 27 21:25:30 2022')

s = sched.scheduler(time.localtime, time.sleep)
s.enterabs(a, 0,action)
s.run()

it only works when the given time is past the current time if it isn't, I get this error

TypeError: unsupported operand type(s) for -: 'time.struct_time' and 'time.struct_time'

What is causing this problem and how can I solve it?



Solution 1:[1]

I usually using this,

https://pypi.org/project/schedule/

and using datetime.isoformat(string) as isoformat datetime.

Solution 2:[2]

The scheduler was not designed to work with date+time structures.

From the docs:

timefunc should be callable without arguments, and return a number (the “time”, in any units whatsoever).

You have to switch e.g. to timestamps (number of seconds since some defined start)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dimz
Solution 2 VPfB