[英]Keep getting this AttributeError: partially initialized module 'schedule' has no attribute 'every'
Is anyone able to help me with this error I'm making a with a Coronavirus tracker twitter bot with python.有人能帮我解决这个错误吗? It basically scrapes data from https://www.worldometers.info/coronavirus/ and post updates on twitter.
它基本上从https://www.worldometers.info/coronavirus/中抓取数据,并在 twitter 上发布更新。
The problem I'm having is to do with the scheduler on line 5 & 24.我遇到的问题与第 5 行和第 24 行的调度程序有关。
Traceback (most recent call last):回溯(最近一次通话最后):
File "C:\Users\Abdul\Documents\CoronavirusBot\twitter_bot.py", line 5, in文件“C:\Users\Abdul\Documents\CoronavirusBot\twitter_bot.py”,第 5 行,在
import schedule进口时间表
File "C:\Users\Abdul\Documents\CoronavirusBot\schedule.py", line 24, in文件“C:\Users\Abdul\Documents\CoronavirusBot\schedule.py”,第 24 行,在
schedule.every().day.at("19:51").do(submit_tweet) schedule.every().day.at("19:51").do(submit_tweet)
AttributeError: partially initialized module 'schedule' has no attribute 'every' (most likely due to a circular import) AttributeError:部分初始化的模块 'schedule' 没有属性 'every'(很可能是由于循环导入)
Twitter_bot.py 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)
This happens your python code file name is 'schedule.py'.这发生在您的 python 代码文件名是“schedule.py”。 If you change the file name to another one, you will no longer see the error.
如果您将文件名更改为另一个,您将不再看到该错误。
you should not use schedule(shcedule.py) for your file name你不应该使用 schedule(shcedule.py) 作为你的文件名
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.