从google.cloud.logging_v2导入类型ImportError:无法导入名称类型

时间:2018-10-04 22:42:11

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

我正在研究在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的问题,但我不确定。

1 个答案:

答案 0 :(得分:2)

这看起来像通用的python client for Stackdriver logging。尚未针对GAE第一代(即python 2.7)应用程序设计/受其支持,因为此运行时已经集成了自己的(自定义)堆栈驱动程序日志记录。

来自Supported environments

  

以下VM实例使用自己的实例支持Stackdriver Logging   软件,可能包括自定义版本或   Stackdriver Logging代理。手动安装Stackdriver Logging   不支持它们上的代理: