关于schedule模块执行定时任务的报错

关于schedule模块执行定时任务的报错

背景

在django项目中,要使用schedule模块来执行定时执行的代码任务,没怎么使用过。便,在网上找些模板代码,先跑一下。结果,却报了一个错误,然后在百度上也没找到有相关的报错信息文章。后来,觉得可能是命名的问题导致的,便试了一下。果然是,拿出来跟大家分享一下,少跳点坑。

1. 安装schedule

pip install schedule

2. 代码模板

import schedule
import time

def job(message='stuff'):
    print("I'm working on:", message)

schedule.every(1).minutes.do(job)
schedule.every(5).to(10).days.do(job)
schedule.every().hour.do(job, message='things')
schedule.every().day.at("10:30").do(job)

while True:
    schedule.run_pending()
    time.sleep(1)

3. 报错信息

Traceback (most recent call last):
  File "/home/python/Desktop/django/schedule.py", line 1, in <module>
    import schedule
  File "/home/python/Desktop/django/schedule.py", line 8, in <module>
    schedule.every(1).minutes.do(job)
AttributeError: module 'schedule' has no attribute 'every'

4. 错误原因

错误的原因是:命名这个文件的时候,命名是跟这个schedule模块包一样的,导致两个文件冲突了,然后就找不到本来安装的schedule模块包了。

猜你喜欢

转载自blog.csdn.net/huazhiliandong/article/details/85243015