APScheduler: LookupError: No trigger by the name "interval" was found

APScheduler: LookupError: No trigger by the name “interval” was found

环境

python: 2.6.6
PyInstaller: 2.1
APScheduler: 开始是3.0.1,后来是3.0.5

问题一

问题描述

以前在别的机器上开发的python程序(python2.7),在新的机器上运行时报错

LookupError: No trigger by the name "interval" was found

程序代码

import os, time
from datetime import datetime
from apscheduler.schedulers.background import BackgroundScheduler

def myjob():
    print('myjob: %s' % datetime.now())
    time.sleep(5)

if __name__ == '__main__':
    scheduler = BackgroundScheduler()
    scheduler.add_job(myjob, 'interval', seconds=1)
    scheduler.start()

    try:
        while True:
            time.sleep(5)
    except (KeyboardInterrupt, SystemExit):
        scheduler.shutdown()

原因

是由于低版本的setuptools导致

解决办法

sudo pip install --upgrade setuptools
sudo pip install --ignore-installed apscheduler

然后再次运行上面的python代码,问题解决。

问题二

问题描述

第一个问题解决后,在运行使用pyinstaller打包生成的可执行文件的时候报错

Traceback (most recent call last):
  File "<string>", line 11, in <module>
  File ".../out00-PYZ.pyz/apscheduler.schedulers.base", line 330, in add_job
  File ".../out00-PYZ.pyz/apscheduler.schedulers.base", line 782, in _create_trigger
  File ".../out00-PYZ.pyz/apscheduler.schedulers.base", line 766, in _create_plugin_instance
LookupError: No trigger by the name "interval" was found

原因

感觉好像是由于pyinstaller打包的时候使用了错误版本的APScheduler。(不确定)???

解决办法

不要在add_job方法中使用“’interval’, seconds=1”做trigger,而是先创建一个IntervalTrigger对象,然后add_job的时候使用这个对象,即:

修改原来代码中

    scheduler.add_job(myjob, 'interval', seconds=1)

 trigger = IntervalTrigger(seconds=1) scheduler.add_job(myjob, trigger)

完整代码如下

def myjob():
    print('myjob: %s' % datetime.now())
    time.sleep(5)

if __name__ == '__main__':
    scheduler = BackgroundScheduler()
    trigger = IntervalTrigger(seconds=1)
    scheduler.add_job(myjob, trigger)
    scheduler.start()

    try:
        while True:
            time.sleep(5)
    except (KeyboardInterrupt, SystemExit):
        scheduler.shutdown()

然后用PyInstaller重新打包,此时再运行可执行文件的时候就不会报错了。

转载请以链接形式标明本文地址
本文地址:http://blog.csdn.net/kongxx/article/details/50501605

你可能感兴趣的:(python)