user15664382
user15664382

Reputation:

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.

The problem I'm having is to do with the scheduler on line 5 & 24.

Traceback (most recent call last):

File "C:\Users\Abdul\Documents\CoronavirusBot\twitter_bot.py", line 5, in

import schedule

File "C:\Users\Abdul\Documents\CoronavirusBot\schedule.py", line 24, in

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)

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)

Upvotes: 1

Views: 8008

Answers (2)

Ali Eyvazi
Ali Eyvazi

Reputation: 21

you should not use schedule(shcedule.py) for your file name

Upvotes: 1

Mark Kim
Mark Kim

Reputation: 101

This happens your python code file name is 'schedule.py'. If you change the file name to another one, you will no longer see the error.

Upvotes: 10

Related Questions