2017-08-26 147 views
0

編輯:這就是爲什麼新來Python的人應該先看書......謝謝指出我真正基本的錯誤,夥計們。NameError:Name'Tweepy'not defined(Python)

我不知道我在做什麼,所以忍着我,在這裏。

我有這樣的代碼:

from credentials import * 

auth = tweepy.OAuthHandler(consumer_key, consumer_secret) 
auth.set_access_token(access_token, access_token_secret) 
api = tweepy.API(auth) 

import tweepy 
from time import sleep 
from credentials import * 

auth = tweepy.OAuthHandler(consumer_key, consumer_secret) 
auth.set_access_token(access_token, access_token_secret) 
api = tweepy.API(auth) 

my_file=open('The Story of the Volsungs.txt','r') 
file_lines=my_file.readlines() 
my_file.close() 

def random_line(afile): 
    line = next(afile) 
    for num, aline in enumerate(afile): 
     if random.randrange(num + 2): continue 
     line = aline 
    return line 

def tweet(): 
    for line in file_lines: 
     try: 
      print(line) 
      if line != '\n': 
       api.update_status(line) 
       sleep(3600) 
      else: 
       pass 
     except tweepy.TweepError as e: 
      print(e.reason) 
      sleep(2) 

tweet() 

,這讓我這個錯誤(用戶名不是*倒是出實際的東西):

Traceback (most recent call last): 
File "C:\\Users\J*****\Desktop\IzzieBot\TweetBot\run_bot.py", line 4, in <module> 
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) 
NameError: name 'tweepy' is not defined 

我不知道什麼是錯做或它需要我改變,錯誤是如此模糊。

+0

你叫'tweepy ...''之前進口tweepy' ... –

+0

您正在嘗試使用Tweepy第三行已經導入之前它。整個大塊似乎是重複的。 – Carcigenicate

回答

1

更改導入順序如下圖所示:

from credentials import * 

import tweepy 
from time import sleep 

auth = tweepy.OAuthHandler(consumer_key, consumer_secret) 
auth.set_access_token(access_token, access_token_secret) 
api = tweepy.API(auth)