部署后我的应用程序报告 - 无法从 'google.cloud' 导入名称 'vision'

Once deployed my app reports - Cannot import name 'vision' from 'google.cloud'

我正在开发 Google 应用程序,但在部署时遇到问题。该项目包含两个服务网站和工作者。该网站充当首页,工作人员通过 cron 运行 每 5 分钟访问一次页面。

我遇到的问题是,当 运行 在本地设置它们时,两个服务 运行 都很好。使用 gcloud 部署应用程序后 returns 日志控制台中出现错误。

错误是:导入错误:无法从 'google.cloud'(未知位置)导入名称 'vision'

这是工作人员服务不工作。 requirements.txt 读

Flask~=1.1.2
google-api-core==1.22.4
google-auth==1.23.0
google-cloud-core==1.4.3
google-cloud-datastore==2.0.0
google-cloud-logging==2.0.0
google-cloud-storage==1.33.0
google-cloud-vision==2.0.0
google-crc32c==1.0.0
google-resumable-media==1.1.0
requests==2.25.0

工人的进口是

import os
import logging
import google.cloud.logging
import google.oauth2.id_token

from flask import Flask, render_template, request
from google.auth.transport import requests
from google.cloud import datastore
from google.cloud import vision

日志浏览器上的完整错误是

Traceback (most recent call last):
  File "/layers/google.python.webserver/gunicorn/gunicorn/arbiter.py", line 583, in spawn_worker
    worker.init_process()
  File "/layers/google.python.webserver/gunicorn/gunicorn/workers/gthread.py", line 92, in init_process
    super().init_process()
  File "/layers/google.python.webserver/gunicorn/gunicorn/workers/base.py", line 119, in init_process
    self.load_wsgi()
  File "/layers/google.python.webserver/gunicorn/gunicorn/workers/base.py", line 144, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/layers/google.python.webserver/gunicorn/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/layers/google.python.webserver/gunicorn/gunicorn/app/wsgiapp.py", line 49, in load
    return self.load_wsgiapp()
  File "/layers/google.python.webserver/gunicorn/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/layers/google.python.webserver/gunicorn/gunicorn/util.py", line 358, in import_app
    mod = importlib.import_module(module)
  File "/opt/python3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/srv/main.py", line 9, in <module>
    from google.cloud import vision
ImportError: cannot import name 'vision' from 'google.cloud' (unknown location)

我已经尝试重新排列导入,因为我读到可能存在一些命名空间问题,但并没有解决它

问题似乎是由需求文件中组件的顺序引起的,类似于this issue。解决方案是在 requirements.txt 文件中显式包含 grpcio 模块,例如:

Flask~=1.1.2
google-api-core==1.22.4
google-auth==1.23.0
google-cloud-core==1.4.3
google-cloud-datastore==2.0.0
google-cloud-logging==2.0.0
google-cloud-storage==1.33.0
google-cloud-vision==2.0.0
google-crc32c==1.0.0
google-resumable-media==1.1.0
requests==2.25.0
grpcio==1.34.0