App Engine 数据存储区中的 GRPC 和类型导入错误

GRPC and types import error in App Engine Datastore

我在 App Engine 中成功部署了我的应用程序。但是,当我尝试访问我的 API 时,我收到以下消息:

Error: Server Error The server encountered an error and could not complete your request. Please try again in 30 seconds.

我无法理解此问题的原因,因为它来自数据存储 API。有人可以帮我解决这个问题吗?

以下是所有相关信息。

回溯

当提到根的时候URL

2018-03-27 08:08:45,090 <root> [ERROR]
2018-03-27 16:08:45.099 HKT
Traceback (most recent call last):
2018-03-27 16:08:45.099 HKT
  File "/base/alloc/tmpfs/dynamic_runtimes/python27/6baee03590d046f1_unzipped/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 240, in Handle
2018-03-27 16:08:45.100 HKT
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
2018-03-27 16:08:45.100 HKT
  File "/base/alloc/tmpfs/dynamic_runtimes/python27/6baee03590d046f1_unzipped/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
2018-03-27 16:08:45.100 HKT
    handler, path, err = LoadObject(self._handler)
2018-03-27 16:08:45.100 HKT
  File "/base/alloc/tmpfs/dynamic_runtimes/python27/6baee03590d046f1_unzipped/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 96, in LoadObject
2018-03-27 16:08:45.100 HKT
    __import__(cumulative_path)
2018-03-27 16:08:45.100 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/web/app.py", line 6, in <module>
2018-03-27 16:08:45.100 HKT
    from web.routes import ROUTES_BLUEPRINT
2018-03-27 16:08:45.100 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/web/routes.py", line 7, in <module>
2018-03-27 16:08:45.100 HKT
    from metadata.repository.gcp_clouddatastore import GCPCloudDataStore
2018-03-27 16:08:45.100 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/metadata/repository/gcp_clouddatastore.py", line 8, in <module>
2018-03-27 16:08:45.100 HKT
    from google.cloud import datastore
2018-03-27 16:08:45.100 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/lib/google/cloud/datastore/__init__.py", line 60, in <module>
2018-03-27 16:08:45.100 HKT
    from google.cloud.datastore.batch import Batch
2018-03-27 16:08:45.100 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/lib/google/cloud/datastore/batch.py", line 24, in <module>
2018-03-27 16:08:45.100 HKT
    from google.cloud.datastore import helpers
2018-03-27 16:08:45.100 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/lib/google/cloud/datastore/helpers.py", line 29, in <module>
2018-03-27 16:08:45.100 HKT
    from google.cloud.datastore_v1.proto import datastore_pb2
2018-03-27 16:08:45.100 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/lib/google/cloud/datastore_v1/__init__.py", line 18, in <module>
2018-03-27 16:08:45.100 HKT
    from google.cloud.datastore_v1.gapic import datastore_client
2018-03-27 16:08:45.100 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/lib/google/cloud/datastore_v1/gapic/datastore_client.py", line 18, in <module>
2018-03-27 16:08:45.100 HKT
    import google.api_core.gapic_v1.client_info
2018-03-27 16:08:45.100 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/lib/google/api_core/gapic_v1/__init__.py", line 16, in <module>
2018-03-27 16:08:45.100 HKT
    from google.api_core.gapic_v1 import config
2018-03-27 16:08:45.100 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/lib/google/api_core/gapic_v1/config.py", line 23, in <module>
2018-03-27 16:08:45.100 HKT
    import grpc
2018-03-27 16:08:45.100 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/lib/grpc/__init__.py", line 22, in <module>
2018-03-27 16:08:45.100 HKT
    from grpc._cython import cygrpc as _cygrpc
2018-03-27 16:08:45.100 HKT
ImportError: dynamic module does not define init function (initcygrpc)
2018-03-27 16:08:45.111 HKT
This request caused a new process to be started for your application, and thus caused your application code to be loaded for the first time. This request may thus take longer and use more CPU than a typical request for your application.

访问 API 除了 root 之外,显示另一个回溯

2018-03-27 16:13:41.378 HKT
2018-03-27 08:13:41,377 <root> [ERROR] 
2018-03-27 16:13:41.378 HKT
Traceback (most recent call last):
2018-03-27 16:13:41.378 HKT
  File "/base/alloc/tmpfs/dynamic_runtimes/python27/6baee03590d046f1_unzipped/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 240, in Handle
2018-03-27 16:13:41.378 HKT
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
2018-03-27 16:13:41.378 HKT
  File "/base/alloc/tmpfs/dynamic_runtimes/python27/6baee03590d046f1_unzipped/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
2018-03-27 16:13:41.378 HKT
    handler, path, err = LoadObject(self._handler)
2018-03-27 16:13:41.378 HKT
  File "/base/alloc/tmpfs/dynamic_runtimes/python27/6baee03590d046f1_unzipped/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 96, in LoadObject
2018-03-27 16:13:41.378 HKT
    __import__(cumulative_path)
2018-03-27 16:13:41.378 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/web/app.py", line 6, in <module>
2018-03-27 16:13:41.378 HKT
    from web.routes import ROUTES_BLUEPRINT
2018-03-27 16:13:41.378 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/web/routes.py", line 7, in <module>
2018-03-27 16:13:41.378 HKT
    from metadata.repository.gcp_clouddatastore import GCPCloudDataStore
2018-03-27 16:13:41.378 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/metadata/repository/gcp_clouddatastore.py", line 8, in <module>
2018-03-27 16:13:41.378 HKT
    from google.cloud import datastore
2018-03-27 16:13:41.378 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/lib/google/cloud/datastore/__init__.py", line 60, in <module>
2018-03-27 16:13:41.378 HKT
    from google.cloud.datastore.batch import Batch
2018-03-27 16:13:41.378 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/lib/google/cloud/datastore/batch.py", line 24, in <module>
2018-03-27 16:13:41.378 HKT
    from google.cloud.datastore import helpers
2018-03-27 16:13:41.378 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/lib/google/cloud/datastore/helpers.py", line 29, in <module>
2018-03-27 16:13:41.378 HKT
    from google.cloud.datastore_v1.proto import datastore_pb2
2018-03-27 16:13:41.378 HKT
  File "/base/data/home/apps/s~singapore-profile-dev2/20180327t160251.408595751204823647/lib/google/cloud/datastore_v1/__init__.py", line 17, in <module>
2018-03-27 16:13:41.378 HKT
    from google.cloud.datastore_v1 import types
2018-03-27 16:13:41.378 HKT
ImportError: cannot import name types

app.yaml 文件内容

runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: web.app.app
libraries:
- name: lxml
  version: latest

appengine_config.py 文件内容

# appengine_config.py
from google.appengine.ext import vendor

# Add any libraries install in the "lib" folder.
vendor.add('lib')

您正在使用数据存储客户端库,但这在标准环境中不起作用。来自 docs:

You cannot use the Cloud Datastore client library with Python applications in the App Engine standard environment.

您需要按照文档中的建议使用 ndb 包(我相信还有一个 REST API,但我从未使用过)。

第一个回溯似乎表明 grpc 以某种方式参与其中,这是一个问题。根据对 issue 149 的评论,grpc 尚不支持 GAE 标准环境。

但是由于您使用的是 GAE 标准环境,因此您应该真正使用推荐的 GAE-optimized(以及更多 feature-rich)NDB Client Library 来访问数据存储,而不是通用的 google.cloud.datastore 一个(可能 确实包含不符合标准环境沙箱限制的功能)。

(2020 年 7 月) Google 在 2019 年解决了这个问题,所以现在,Python 2.7 应用 运行 标准版可以使用 google.cloud.datastore图书馆。要消除 grpc 错误,请将这些行添加到 app.yaml 文件的 libraries 部分(如果不存在则添加):

libraries:
- name: grpcio
  version: 1.0.0

已接受答案中的声明:“您不能在 App Engine 标准环境中将 Cloud Datastore 客户端库与 Python 应用程序一起使用,”不再正确并已从文档中删除。

如果移植到 Cloud Datastore from the App Engine NDB library ( google.appengine.ext.ndb), it may be easier to first port to the Cloud NDB library (google.cloud.ndb, which is really just an "NDB client library" for Cloud Datastore). The step of adding the grpcio reference to app.yaml is described on this migration page

此中间端口的另一个原因是 Cloud NDB 在 Python 3 中也可用,让您有机会将您的应用程序从 2.x 迁移到 3.x 并且仍然有东西工作。此外,您还可以获得使用 App Engine 的第二代运行时的额外好处(Gen1 std 仅支持 2.7,而 Flex 支持 2.7 或 3.x,而 Gen2 仅支持 3.x。)

最后从那里,端口到 Cloud Datastore (google.cloud.datastore)。这个中间步骤基本上让您的“应用程序基础设施”为 Cloud Datastore 做好准备,您的大部分繁重工作将是将 NDB 数据 CRUD 迁移到 Cloud Datastore,但不必担心 s/b 处理的这些库问题通过上述中间步骤。 (我将尝试对所有这些进行撰写,并在我得到一些东西时更新此答案。)