首页 > 解决方案 > 不断收到这个 AttributeError:部分初始化的模块 'schedule' 没有属性 'every'

问题描述

有人能帮我解决这个错误吗?它基本上从https://www.worldometers.info/coronavirus/抓取数据并在 Twitter 上发布更新。

我遇到的问题与第 5 行和第 24 行的调度程序有关。

回溯(最近一次通话最后):

文件“C:\Users\Abdul\Documents\CoronavirusBot\twitter_bot.py”,第 5 行,在

进口时间表

文件“C:\Users\Abdul\Documents\CoronavirusBot\schedule.py”,第 24 行,在

schedule.every().day.at("19:51").do(submit_tweet)

AttributeError:部分初始化的模块 'schedule' 没有属性 'every'(很可能是由于循环导入)

Twitter_bot.py

from config import CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET
import tweepy
import requests
import schedule
import time
from lxml import html


def create_tweet():
    response = requests.get('https://www.worldometers.info/coronavirus/')
    doc = html.fromstring(response.content)
    total, deaths, recovered = doc.xpath('//div[@class="maincounter-number"]/span/text()')

    tweet = f'''Coronavirus Latest Updates
Total cases: {total}
Recovered: {recovered}
Deaths: {deaths}

Source: https://www.worldometers.info/coronavirus/

#coronavirus #covid19 #coronavirusnews #coronavirusupdates
'''
    return tweet


if __name__ == '__main__':
    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

    # Create API object
    api = tweepy.API(auth)

    try:
        api.verify_credentials()
        print('Authentication Successful')
    except:
        print('Error while authenticating API')
        sys.exit(1)
    while True:
        schedule.run_pending()
        time.sleep(1)

    tweet = create_tweet()
    api.update_status(tweet)
    print('Tweet successful')
else:
   print('error') ``` 

 


 **Schedule.py**
```import schedule

#define function create tweet

#auth and create tweet
def submit_tweet(*args, **kwargs): #Add the needed args here
    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

    # Create API object
    api = tweepy.API(auth)

    try:
        api.verify_credentials()
        print('Authentication Successful')
    except:
        print('Error while authenticating API')
        sys.exit(1)

    tweet = create_tweet()
    api.update_status(tweet)
    print('Tweet successful')

schedule.every().day.at("21:19").do(submit_tweet)

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

标签: python

解决方案


这发生在你的 python 代码文件名是'schedule.py'。如果您将文件名更改为另一个,您将不再看到错误。


推荐阅读