Search code examples
pythonspyderapscheduler

apsheduler calling a python script is generating a "CoInitialize has not been called."


I'm trying to run a python script every 30 minutes:

from apscheduler.schedulers.background import BackgroundScheduler
sched = BackgroundScheduler()
def job():
    runfile('C:/temp/KIC53_Monitor.py', 
    wdir='C:/temp')
sched.add_job(job, 'cron', minute='15,45', second=0)
sched.start()

I'm getting this error:

IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch) pywintypes.com_error: (-2147221008, 'CoInitialize has not been called.', None, None)

I thought it was pretty straight forward, but this is the first time I've used apscheduler, so what do I know.

Any help would be appreciated

Thanks


Solution

  • The issue, was both apscheduler and the python script it was scheduling. The Python script contained this import statement:

    import win32com.client as com
    

    When the import statement is removed, apscheduler works fine, (but the python script becomes useless. Not an optimal solution). The issue now becomes, "How do I import a com process without breaking apscheduler?"...

    but that's a different issue.