ImportError: cannot import name 'pubsub_v1'

2019-07-17 22:26发布

问题:

I am trying to import the pubsub_v1 in a cloud function. But when I tried to deploy it on GCP, the problem as in the title coming out. The requirements.txt file is in the same directory as the main.py file. Here is what in the requirements.txt:

google-api-core==1.3.0
google-auth==1.5.1
google-cloud-core==0.28.1
google-cloud-storage==1.10.0
google-resumable-media==0.3.1
googleapis-common-protos==1.5.3
google-api-python-client==1.7.4
oauth2client==4.1.2
google-cloud-bigquery==1.5.0
google-cloud-logging==1.7.0
google-cloud-pubsub==0.26.0
proto-google-cloud-pubsub-v1==0.15.4
gapic-google-cloud-pubsub-v1==0.15.4
grpc-google-iam-v1==0.11.4

I used the following command to deploy the cloud function:

gcloud functions deploy some_function --runtime python37 --trigger-resource bucket --trigger-event google.storage.object.finalize --memory 2048 --timeout 500

回答1:

You're using a pretty old version of google-cloud-pubsub. You'll only be able to do:

from google.cloud import pubsub_v1

with google-cloud-pubsub>=0.28.1.

Also, it might worth mentioning that you probably don't need to list all the sub-dependencies (like gapic-google-cloud-pubsub-v1) in your requirements.txt -- when you deploy your function, Google Cloud Functions will resolve all these for you just by specifying google-cloud-pubsub.