2012-04-08 61 views
0

更改導入的來自進口 我遇到這個錯誤後:date.timestamp沒有在python中找到?

from datetime import datetime, date, timedelta 
today = date.today() 
from time import mktime 
from feedparser import feedparser 
import settings 

def check_calendar(): 
    d = feedparser.parse(settings.personal_calendar_feed) 
    for entry in d.entries: 
     if(date.fromtimestamp(mktime(entry.date_parsed))==today): 

Traceback (most recent call last): 
    File computer.py", line 734, in <module> 
    check_calendar() 
    File "computer.py", line 210, in check_calendar 
    if(date.fromtimestamp(mktime(entry.date_parsed))==today): 
AttributeError: 'function' object has no attribute 'fromtimestamp' 
+0

什麼是這個模塊'日期'? – Abhijit 2012-04-08 20:18:46

+0

@Abhijit:'日期'不是一個模塊。這是'datetime'模塊中的一個類 – jdi 2012-04-08 20:29:42

+0

@jdi:我誤讀了第一行,錯過了'from datetime' – Abhijit 2012-04-08 20:32:19

回答

3

它說

AttributeError: 'function' object has no attribute 'fromtimestamp'

錯誤

。顯然,你的代碼中可能有一個名爲「date」的函數。由於Python允許您使用任何名稱,並且新的衝突名稱將覆蓋較舊的名稱。

相反,當Python不能找到一個模塊或對象的函數,它通常說類型的對象有沒有屬性或模塊沒有屬性,就像如果我想打電話給「fromtimes」:

type object 'datetime.date' has no attribute 'fromtimes'

您可能需要再次仔細檢查您的代碼。

+0

你絕對是對的!現在我要重命名這個函數。順便說一句,這個例外是不是很具描述性... ... – eadmaster 2012-04-08 23:25:19

3

這極有可能是你已經重新聲明date作爲功能def date():前面的代碼。否則它是沒有意義的。