2016-07-25 65 views
2

我正在調試python代碼(python2.7.12),因爲我的代碼工作正常,但是在向數據庫中傳送推文時,所有變量都爲NULL。異常AttributeError:''NoneType'對象沒有屬性'路徑'「

我得到的錯誤是:

Exception AttributeError: "'NoneType' object has no attribute 'path'" in <function _remove at 0x10068f140> ignored 

我假定這是錯誤的代碼如下:

def put_tweets_in_database(tweets): 
    print "putting tweets in database" 
    errors = 0 
    count = 0 

    for tweet in tweets: 
     try: 
      commit_tweet_to_database(tweet, count, len(tweets)) 
      count += 1 
     except Exception as e: 
      print e 
      session.rollback() 
      errors += 1 
    print 'there were {} errors'.format(errors) 

我不認爲功能commit_tweet_to_database()是錯誤的...

你有什麼想法...?我將不勝感激任何幫助!

謝謝。

+0

我打賭上下文中沒有用戶定義的'_remove',沒有錯誤的行號,並且在循環迭代之間引發異常。所以我懷疑它與循環迭代器有關,可能是Python中的一個錯誤。 –

+0

注意:只有在使用'pdb'(無韻)打開代碼時,纔會出現錯誤。 –

回答

0
Exception AttributeError: "'NoneType' object has no attribute 'path'" in <function _remove at 0x10068f140> ignored 

這告訴在功能_remove屬性path被試圖將一個NoneType對象上訪問。 NoneType對象沒有屬性。因此,您可能需要查看_remove函數並從那裏開始調試。

-1

我有同樣的錯誤,這裏是我的情況:

瀏覽器= webdriver.Firefox()

browser.get( 'http://www.google.com')

打印browser.title

  • 這下面會給我錯誤信息:'NoneType'對象沒有'路徑'屬性

browser.quit()

  • 這下面不會給錯誤

browser.close()

所以,問題是,你有使用了錯誤的方法,你的對象!

+0

close()不同於quit()...你不能只交換它們 –

2

我也在處理這個錯誤。我嘗試使用browser.close()方法,雖然它停止了 - 'NoneType'對象沒有屬性'路徑' - 從顯示,我剩下一堆開放的Firefox瀏覽器實例。

.close()方法關閉chrome,並且它不會在Firefox中拋出NoneType錯誤,但它會使Firefox打開。 .quit()方法關閉了這兩個瀏覽器,但它引發了Firefox的錯誤。

我爲我的代碼使用django的StaticLiveServerTestCase類。

我寫了一個小調試器循環來測試事情。只需取消註釋並註釋掉.quit()和.close()語句。

class BaseTestCase(StaticLiveServerTestCase): 

    @classmethod 
    def setUp(self): 

     self.firefox = webdriver.Firefox() 
     self.chrome = webdriver.Chrome() 
     self.browsers = [self.firefox, self.chrome] 

    @classmethod 
    def tearDown(self): 

     for browser in self.browsers: 
      if browser == self.firefox: 
       print('firefox') 
       browser.close() 
       # browser.quit() 
      elif browser == self.chrome: 
       print('chrome') 
       browser.close() 
       # browser.quit() 

我還是不知道答案,但我認爲這是朝正確方向邁出的一步。

+0

現在我想知道原始問題是否真的由@Dung和我自己回答。 – Pat

+0

這不是真正的原始問題的答案......雖然它是來自selenium.browser.quit()的Python Selenium 3.x模塊的一個例外。 – RVT

+0

close()不同於quit()...你不能只交換它們 –

0

聽起來好像儘管您的「嘗試」子句失敗,導致打印異常?我可能會爲Exception catch添加更多調試,例如將參數打印到commit_tweet_to_database,只是爲了確保傳遞可行的參數。

相關問題