2017-05-03 87 views
1

我見過這個問題的許多迭代,以及許多非常具體和無用的ansers。現在的問題是這樣的:GAE devappserver2 TypeError:不支持的操作數類型爲 - :'datetime.datetime'和'NoneType'

當運行dev_appserver.py app.yaml我得到一個錯誤,像這樣:

waxed_backend_1  | _run_file(__file__, globals()) 
waxed_backend_1  | File "/google-cloud-sdk/platform/google_appengine/dev_appserver.py", line 97, in _run_file 
waxed_backend_1  |  execfile(_PATHS.script_file(script_name), globals_) 
waxed_backend_1  | File "/google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/devappserver2.py", line 899, in <module> 
waxed_backend_1  |  main() 
waxed_backend_1  | File "/google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/devappserver2.py", line 895, in main 
waxed_backend_1  |  dev_server.stop() 
waxed_backend_1  | File "/google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/devappserver2.py", line 775, in stop 
waxed_backend_1  |  metrics.GetMetricsLogger().Stop() 
waxed_backend_1  | File "/google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/metrics.py", line 123, in Stop 
waxed_backend_1  |  total_run_time = int((Now() - self._start_time).total_seconds()) 
waxed_backend_1  | TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'NoneType' 

一個人怎麼調試呢?

回答

2

在裏面提到的錯誤的metrics.py腳本,你會看到這樣的內容:

class _MetricsLogger(object): 
    """Logs metrics for the devappserver to Google Analytics.""" 

    def __init__(self): 
    """Initializes a _MetricsLogger.""" 
    self._client_id = None 
    self._user_agent = None 
    self._runtimes = None 
    self._start_time = None 
    self._log_once_on_stop_events = {} 

    def Start(self, client_id, user_agent=None, runtimes=None): 
    """Starts a Google Analytics session for the current client. 

    Args: 
     client_id: A string Client ID representing a unique anonyized user. 
     user_agent: A string user agent to send with each log. 
     runtimes: A set of strings containing the runtimes used. 
    """ 
    self._start_time = Now() 
    self._client_id = client_id 
    self._user_agent = user_agent 
    self._runtimes = ','.join(sorted(list(runtimes))) if runtimes else None 
    self.Log(DEVAPPSERVER_CATEGORY, START_ACTION) 

    def Stop(self): 
    """Ends a Google Analytics session for the current client.""" 

    total_run_time = int((Now() - self._start_time).total_seconds()) ###### <<<<< ERROR HERE 

    self.LogOnceOnStop(DEVAPPSERVER_CATEGORY, STOP_ACTION, value=total_run_time) 
    self.LogBatch(self._log_once_on_stop_events.itervalues()) 

etc 

_start_time被初始化爲None,從來沒有更新。這是因爲在調用Start之前引發了一些異常。然後gae吃了我們的追蹤。 Nom nom。

所以要調試這個錯誤,最好的辦法是檢索我們的回溯。我只是編輯我metrics.py停止功能看起來像這樣:

def Stop(self): 
    """Ends a Google Analytics session for the current client.""" 
    import traceback 
    sErr = traceback.format_exc() 
    if sErr: 
     print(sErr) 
     print('===========') 

    total_run_time = int((Now() - self._start_time).total_seconds()) 

    self.LogOnceOnStop(DEVAPPSERVER_CATEGORY, STOP_ACTION, value=total_run_time) 
    self.LogBatch(self._log_once_on_stop_events.itervalues()) 

現在它打印出原始的錯誤

+1

對於發現這個線程其他人的回溯:@Sheena是正確的,這意味着在'_start_time'啓動之前拋出錯誤,在我的情況下,app.yaml沒有正確的格式/必要的值。這會導致dev_appserver.py在正確啓動之前失敗。檢查你的app.yaml。 – Mathias

+1

@Mathias我從幾個不同的事情得到了這個錯誤。通常是因爲Python代碼中的錯誤,而不是yaml。雖然我確實認爲錯誤可能出現在配置中,但這只是衆多需要查看的地方之一。尋找異常的最佳地點通常是回溯。否則就是檢查自上次運行後編輯的內容,然後再深入研究。大多數情況下,這並不好玩 – Sheena

相關問題