【问题标题】:ImportError: cannot import name cygrpcImportError:无法导入名称 cygrpc
【发布时间】:2019-10-18 10:44:39
【问题描述】:

我正在尝试在 Google App Engine - Standard 上使用 Firebase 本机模式。我的 Python 语言是 Python 2.7。当我尝试从 google.cloud import firestore 时,出现错误 ImportError: cannot import name cygrpc

我已经部署了文档 here 中描述的 virtualenv。

pip install virtualenv
virtualenv env
source env/bin/activate

我的 appengine_config.py 是

from google.appengine.ext import vendor
import os.path

# Add any libraries installed in the "lib" folder.
vendor.add('lib')
vendor.add(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'lib'))

my_app.py 包括

from google.appengine.ext.webapp import template
from google.appengine.ext import ndb
from google.appengine.api import mail

import os

from google.cloud import firestore



(/base/alloc/tmpfs/dynamic_runtimes/python27g/43d5822312de17fd/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py:269)
Traceback (most recent call last):
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/43d5822312de17fd/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 240, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/43d5822312de17fd/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 311, in _LoadHandler
    handler, path, err = LoadObject(self._handler)
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/43d5822312de17fd/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 85, in LoadObject
    obj = __import__(path[0])
  File "/base/data/home/apps/s~openbarn-prod/20190602t102855.418624175446659791/main.py", line 4, in <module>
    from controllers import server, common, header
  File "/base/data/home/apps/s~openbarn-prod/20190602t102855.418624175446659791/controllers/server.py", line 10, in <module>
    from google.cloud import firestore
  File "/base/data/home/apps/s~openbarn-prod/20190602t102855.418624175446659791/lib/google/cloud/firestore.py", line 18, in <module>
    from google.cloud.firestore_v1 import __version__
  File "/base/data/home/apps/s~openbarn-prod/20190602t102855.418624175446659791/lib/google/cloud/firestore_v1/__init__.py", line 22, in <module>
    from google.cloud.firestore_v1._helpers import GeoPoint
  File "/base/data/home/apps/s~openbarn-prod/20190602t102855.418624175446659791/lib/google/cloud/firestore_v1/_helpers.py", line 21, in <module>
    import grpc
  File "/base/data/home/apps/s~openbarn-prod/20190602t102855.418624175446659791/lib/grpc/__init__.py", line 23, in <module>
    from grpc._cython import cygrpc as _cygrpc
ImportError: cannot import name cygrpc

我的问题 - 您可以使用 Python 2.7 在 Google App Engine Standard 上使用 Firestore 本机模式吗?我需要 GAE- Standard,因为我们使用的是 GAE-Flex 不支持的 Google Endpoint。

文档here 说 Python2.7 GAE 标准环境不支持 App Engine 客户端库集成。但我不是在尝试 App Engine 客户端库,而是在 GAE 标准环境中尝试 App Engine 服务器库。 如何解决 cygrpc 的导入错误?解决方案here 说-

python -m pip install grpcio --ignore-installed

这是推荐的吗?

【问题讨论】:

    标签: python-2.7 google-app-engine google-cloud-firestore


    【解决方案1】:

    不久前,GAE 标准不支持 GRPC,请参阅 GRPC and types import error in App Engine Datastore。从那以后我没有尝试过,但我在issue 149 上没有看到更新的活动。

    此外,回溯中的 cython 引用表明它可能包含已编译的代码,这将违反适用于您部署的代码的 pure python 标准环境沙箱限制。

    【讨论】:

    • Dan - 您能否确认我们不能使用 Python 2.7 在 Google App Engine Standard 上使用 Firestore 本机模式?
    • 没有客户端库(和用于通信的 GRPC)我很确定这就是它的意思。我直接跳到回溯,实际上错过了主要问题;)
    • 您可以使用 Firestore REST API
    【解决方案2】:

    现在是 2020 年,您现在可以在带有 Python 2.7 的 App Engine 上使用带有 Cloud Firestore 的 grpcio(无需自己明确添加,因为它是一个内置库)。完成这项工作的三件事:

    1. google-cloud-firestore 添加到您的requirements.txt 文件并使用pip install -U -t lib -r requirements.txt 进行安装。
    2. 将这些行添加到您的app.yaml 文件的libraries 部分:
    libraries:
    - name: grpcio
      version: 1.0.0
    - name: setuptools
      version: 36.6.0
    
    1. 确保这些行在您的 appengine_config.py 文件中:
    import pkg_resources
    from google.appengine.ext import vendor
    
    # Set path to your libraries folder.
    path = 'lib'
    # Add libraries installed in the path folder.
    vendor.add(path)
    # Add libraries to pkg_resources working set to find the distribution.
    pkg_resources.working_set.add_entry(path)
    

    当然,我们确实建议您最终migrate to Python 3 以利用下一代(Python App Engine)提供的更大灵活性,尤其是。利用其他 GCP 服务的能力。 但是,如果您的应用程序复杂且严重依赖于 App Engine 的第一代内置服务,则需要注意这样的移植并非没有困难。以上建议大部分来源于this section of the migration docs

    【讨论】:

      【解决方案3】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-03-22
        • 1970-01-01
        • 2018-09-19
        • 2016-03-31
        • 2014-10-10
        • 2014-09-20
        • 2014-08-28
        • 2014-06-10
        相关资源
        最近更新 更多