GAE devappserver2 TypeError:不支持的操作数类型 - :' datetime.datetime'和' NoneType'

时间:2017-05-03 12:49:51

标签: python google-app-engine

我已经看过很多关于这个问题的迭代,以及许多非常具体和无用的激光器。问题是这样的:

运行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'

如何调试这个?

1 个答案:

答案 0 :(得分: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初始化为无,并且从未更新过。这是因为在调用Start之前引发了一些异常。然后吃了我们的追溯。 Nom nom。

因此,要调试此错误,最好的方法是检索我们的回溯。我刚刚编辑了我的metrics.py Stop函数,如下所示:

  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())

现在它打印出原始错误的追溯