简体   繁体   中英

Google Cloud Function - ImportError: cannot import name 'pubsub' from 'google.cloud' (unknown location)

I am deploying a Google Cloud Function that will kick off other Google Cloud Functions using google.cloud.pubsub_v1 and I'm getting this error ImportError: cannot import name 'pubsub' from 'google.cloud' (unknown location)

The beginning of my requirements.txt file looks like this

# Function dependencies, for example:
# package>=version
google-cloud-pubsub
....

The beginning of my main.py script looks like this:

import base64
import json
from google.cloud import pubsub_v1

publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(<PROJECT_ID>, <PUBSUB_TOPIC>)

I am deploying this code from a Google Cloud Source Repository. I have read through this SO post about my error, but that question appears to be about this error arising in a Client application. My error is being generated by the Google Cloud function itself during the deploy process. I don't have sudo rights to the auto-created VM that Google is using to run my process, do I? I should be able to resolve this issue from the requirements.txt file, but nothing I've tried seems to be working.

What's more frustrating is that when I put this same code in the "Inline editor" on the web-based Google Function editor, I don't get an error. I only get this error when loading the code from the repository.

The current file structure in the repository looks something like this:

.
├── package
|   ├── main.py
|   ├── script1.py
|   └── script2.py
├── package2
├── ...
└── requirements.txt

I moved main.py inside of a package because of issues I was having in this SO Question

Any ideas on how to resolve this import error?

Your main.py file and requirements.txt file should be in the same directory, and this should also be the same directory you're deploying your function from.

Also, the google-cloud package is deprecated and shouldn't be used with other google-cloud-* packages. You should remove it from your requirements.txt file.

To install the google-cloud library, you need to perform

pip install google-cloud-storage

as can be seen in the official Google Cloud documentation, so don't install google-cloud-pubsub .

Nevertheless, you import the pubsub package as you did by

from google.cloud import pubsub_v1

Again, a page dedicated for the pubsub_v1 library exists on the official Google Cloud documentation here that shows the following example:

import time

from google.cloud import pubsub_v1

# TODO project_id = "Your Google Cloud Project ID"
# TODO subscription_name = "Your Pub/Sub subscription name"

subscriber = pubsub_v1.SubscriberClient()
# The `subscription_path` method creates a fully qualified identifier
# in the form `projects/{project_id}/subscriptions/{subscription_name}`
subscription_path = subscriber.subscription_path(
    project_id, subscription_name)

def callback(message):
    print('Received message: {}'.format(message))
    message.ack()

subscriber.subscribe(subscription_path, callback=callback)

# The subscriber is non-blocking. We must keep the main thread from
# exiting to allow it to process messages asynchronously in the background.
print('Listening for messages on {}'.format(subscription_path))
while True:
    time.sleep(60)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM