Search code examples
pythongoogle-cloud-platformgoogle-cloud-vision

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


I am working on a Google App and have an issue when I deploy it. The project contains two services website and worker. The website acts as a front page and the worker is run through a cron and has its page hit every 5 minutes.

The issue I'm having is that when running them locally both services run fine. Once I deploy with gcloud the application returns an error in the Logs Console.

The error is: ImportError: cannot import name 'vision' from 'google.cloud' (unknown location)

It is the worker service that is not working. It's requirements.txt reads

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

The imports for the worker are

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

The full error on the Logs explorer is

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)

I've tried rearranging the imports as I read that there could be some namespace issues but that didn't fix it


Solution

  • The issue seemed to be caused by the order of the components in the requirements file, similar to this issue. The solution is to explicitly include the grpcio module in the requirements.txt file, e.g. :

    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