Scheduler not working in python

Hey All -

I am trying to schedule a run for my repl and I just can’t get it to work. This is my first time posting and I would love your help.

Some background:

  • It’s pyton
  • main.py works
  • the scheduler will run and output the time
  • I have asked the ghostwriter and have no idea
import os
import schedule
import time
from datetime import datetime
import pytz

# Define the time zone for your region
tz = pytz.timezone('America/New_York')

def run_main():
    current_time = datetime.now(tz).strftime('%Y-%m-%d %H:%M:%S')
    print(f"{current_time}  Running main.py...")
    os.system("python main.py")

def tester():
    print('hello world')

def schedule_main():
  # Announce it has been called
  current_time = datetime.now(tz).strftime('%Y-%m-%d %H:%M:%S')
  print(f"Time zone is set to {tz}")
  print(f"called -- {current_time}")
  # Define the schedule for every weekday at 6am, 12pm, and 6pm EST

  schedule.every().day.at("06:00:00").do(run_main)
  schedule.every().day.at("12:00:00").do(run_main)
  schedule.every().day.at("21:35:00").do(tester)
    
  print('Done with schedule')

  # Keep running scheduled jobs in the background
  while True:
      schedule.run_pending()
      time.sleep(1)
      new_time = datetime.now(tz).strftime('%Y-%m-%d %H:%M:%S')
      print(f'{new_time}')

Hello @burkewise! Please edit your post’s code to be formatted like this:

```
Your code here
```

So that it is easier for staff and members of the community to help you with your issue!

Also see this guide on how to share your code:

PS: if you cannot edit your post, please read around a little to increase your trust level and let you access editing your posts.

Hmm, It looks like you aren’t running the functions called schedule_main(), run_main(), or tester(). Try putting at the bottom of your code

schedule_main()
1 Like

Thank you so much for commenting. This is my first time posting for help and I am grateful for your engagement.

I call schedule_main() in main.py, which I believe should call the rest. I have posted main.py below.

import requests
from datetime import datetime

#Check the IP address to make sure google cloud allows it
response = requests.get('https://api.ipify.org')
ip_address = response.text
print(f"The public IP address is: {ip_address}")

# import the functions. Need to be below the IP address so that it doesn't break first 
import jarvis_connections as cxn
import jarvis_functions as fxn
import scheduler as s

if __name__ == "__main__":
  calendard_ids = ['burkewise@gmail.com']
  response = requests.get('https://api.ipify.org')
  ip_address = response.text
  print(f"The public IP address is: {ip_address}")
  service = cxn.authenticate_service()
  for calendar in calendard_ids:
    try:
      events = fxn.get_calendar_events(service, calendar)
      for i, event in enumerate(events):
        try:
          print(f'''\n{i+1}/{len(events)}''')
          #fxn.process_event(event)
        except Exception as e:
          print(e)
    except Exception as e:
          print(e)
  now = datetime.now()
  print(f"\nFinished processing events at {now.strftime('%Y-%m-%d %H:%M:%S')}")

# call schedule_main() here
s.schedule_main()
print("Finished running main.py")

so um do you have the schedule module created or not?

I do. The first message I posted is scheduler.py.

1 Like

the first message… what? the module imports itself??

Maybe I’m not following so apologies in advance if this explanation isn’t relavant.

In scheduler.py, which is the code I posted in my first post, it imports schedule at the top. Then in main.py, which is my second post, it imports scheduler as s in line 13 or so.