0

I am trying to run a script that pulls data from online sources and then emails it to me at specified times. The idea is to run this script from another Python script which uses APScheduler to run the initial script. The reason I wanted to create 2 scripts is because I want to use something like cx_freeze to make an exe file out of the 2nd script which will run in the background of my pc so as not to have to have my IDE open all the time.

My code in the 2nd script looks as follows:

from apscheduler.schedulers.blocking import BlockingScheduler
import initial_script
import os

if __name__ == '__main__':
    scheduler = BlockingScheduler()
    scheduler.add_job(initial_script, trigger='cron', day_of_week='mon-fri', hour='18', minute='30')
    print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))

    try:
        scheduler.start()
    except (KeyboardInterrupt, SystemExit):
        pass

The 'initial_script' is the main python file that actually provides the information I need. The APScheduler code I mostly used from the Github repo here and then I consulted this link to better understand the purpose for using the __name__ = '__main__' code (which I have a feeling is the problem). In the 'initial_script' file I have not used __name__ = '__main__' anywhere.

I have made sure that both files are saved as .py formats - initially I attempted to run the scripts from .py into jupyter notebooks .ipynb but that caused more issues.

The code from the 'initial_script' does run as I am receiving the email when I run it from the 2nd script but the scheduler/trigger gives me an error and does not run.

The error I get now is as follows: TypeError: func must be a callable or a textual reference to one

Please can you assist in explaining what I am doing wrong?

Rip_027
  • 201
  • 2
  • 11

1 Answers1

0

The add_job() method requires that you pass it a callable. You're passing a module and modules cannot be called (you can't do initial_script()). Maybe try passing it a function that is defined in that module?

Alex Grönholm
  • 5,563
  • 29
  • 32
  • Thank you - this worked. I turned my smtp section of the script which emails my data into a function and then called that function as `initial_script.name_of_function` – Rip_027 Aug 04 '22 at 12:20