【问题标题】:from google.cloud.logging_v2 import types ImportError: cannot import name types从 google.cloud.logging_v2 导入类型 ImportError:无法导入名称类型
【发布时间】:2019-03-10 09:36:29
【问题描述】:

我正在开发一个在 GAE 上运行的小应用程序。当我部署我的应用程序或尝试使用 virtualenv(根据文档)时,当我尝试访问该应用程序时,我会在 Cloud Platform 的日志中看到以下内容(它们在本地也相同):

File "/base/data/home/apps/d~my-project/gen-debug:413039252702584887/main.py", line 10, in <module>
    from google.cloud import logging_v2
File "/base/data/home/apps/d~my-project/gen-debug:413039252702584887/lib/google/cloud/logging_v2/__init__.py", line 17, in <module>
    from google.cloud.logging_v2 import types
ImportError: cannot import name types

引发此错误的代码如下所示:

from google.cloud import logging_v2
client = logging_v2.LoggingServiceV2Client()
c = client.list_log_entries(["my-project"], filter_='resource.type="global" AND jsonPayload.hello="there"')
l = list(c)
l1 = l[0]
desc, value = l1.ListFields()[1]
print desc.camelcase_name, dict(value.items())

非常奇怪的是,在 Google 中,我可以进入 Cloud Shell 会话,进入 ipython,并且这段代码完全可以工作。

me@cloudshell:~ (my-project)$ ipython2
Python 2.7.13 (default, Sep 26 2018, 18:42:22)
Type "copyright", "credits" or "license" for more information.
IPython 5.8.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: from google.cloud import logging_v2
...: client = logging_v2.LoggingServiceV2Client()
...: c = client.list_log_entries(["my-project"], filter_='resource.type="global" AND jsonPayload.hello="there"')
...: l = list(c)
...: l1 = l[0]
...: desc, value = l1.ListFields()[1]
...: print desc.camelcase_name, dict(value.items())
...: 
jsonPayload {u'hello': u'there'}

是什么导致在 GAE 上运行它与在 Google Cloud Shell 中运行它不同?此导入错误似乎是 Google 问题,但我不确定。

【问题讨论】:

    标签: python google-app-engine google-cloud-logging


    【解决方案1】:

    这看起来像通用的python client for Stackdriver logging。这不是为 GAE 第一代(即 Python 2.7)应用程序设计/支持的,因为这个运行时已经集成了它自己的(定制的)堆栈驱动程序日志记录。

    来自Supported environments

    以下虚拟机实例使用自己的支持 Stackdriver Logging 软件,可能包括自定义版本或配置 Stackdriver 日志记录代理。手动安装 Stackdriver Logging 不支持代理:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-11
      • 2016-05-29
      • 1970-01-01
      • 2014-10-28
      • 1970-01-01
      相关资源
      最近更新 更多