APScheduler导入错误:没有名为scheduler的模块

12 投票
2 回答
40808 浏览
提问于 2025-04-18 14:31

我遇到了一个导入错误

“ImportError: No module named scheduler”

当我运行以下的Python脚本时:

"""
Demonstrates how to use the blocking scheduler to schedule a job that execute$
"""

from datetime import datetime
import os

from apscheduler.scheduler import BlockingScheduler


def tick():
 print('Tick! The time is: %s' % datetime.now())


if __name__ == '__main__':
 scheduler = BlockingScheduler()
 scheduler.add_job(tick, 'interval', seconds=3)
 print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'$

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

我已经通过以下命令安装了APScheduler: sudo pip install apscheduler

我还使用了以下命令进行了升级: sudo pip install apscheduler --upgrade 另外,我也通过“sudo apt-get install update && sudo apt-get upgrade”升级了我的系统

2 个回答

31

我也遇到了同样的问题,不过后来我发现,

我安装的是apscheduler的3版本,后来我把它换成了2.1.2版本,使用了下面的命令,

pip uninstall apscheduler
pip install apscheduler==2.1.2

在切换到2.1.2版本之前,最好先检查一下,如果你想用3版本里新增的额外功能。不过对我来说,我并不需要太多这些功能。

13

你的导入写错了。应该是:

from apscheduler.schedulers.blocking import BlockingScheduler

参考示例可以在这里找到:

"""
Demonstrates how to use the blocking scheduler to schedule a job that executes on 3 second
intervals.
"""

from datetime import datetime
import os

from apscheduler.schedulers.blocking import BlockingScheduler


def tick():
    print('Tick! The time is: %s' % datetime.now())


if __name__ == '__main__':
    scheduler = BlockingScheduler()
    scheduler.add_job(tick, 'interval', seconds=3)
    print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))

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

撰写回答