【问题标题】:Google Cloud Function - ImportError: cannot import name 'pubsub' from 'google.cloud' (unknown location)Google Cloud Function - ImportError:无法从“google.cloud”(未知位置)导入名称“pubsub”
【发布时间】:2019-02-26 08:49:40
【问题描述】:

我正在部署一个谷歌云函数,它将使用google.cloud.pubsub_v1 启动其他谷歌云函数,我收到了这个错误ImportError: cannot import name 'pubsub' from 'google.cloud' (unknown location)

我的 requirements.txt 文件的开头是这样的

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

我的 main.py 脚本的开头如下所示:

import base64
import json
from google.cloud import pubsub_v1

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

我正在从 Google Cloud Source Repository 部署此代码。我已通读this SO post 关于我的错误,但该问题似乎与客户端应用程序中出现的此错误有关。我的错误是在部署过程中由 Google Cloud 功能本身生成的。我没有sudo 对 Google 用来运行我的进程的自动创建的 VM 的权限,是吗?我应该能够从requirements.txt 文件中解决此问题,但我尝试过的任何方法似乎都不起作用。

更令人沮丧的是,当我将相同的代码放入基于网络的 Google 函数编辑器的“内联编辑器”中时,我没有收到错误消息。从存储库加载代码时,我只会收到此错误。

存储库中的当前文件结构如下所示:

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

由于this SO Question 中遇到的问题,我将 main.py 移到了一个包中

有关如何解决此导入错误的任何想法?

【问题讨论】:

    标签: python git google-cloud-platform google-cloud-functions google-cloud-pubsub


    【解决方案1】:

    您的main.py 文件和requirements.txt 文件应该在同一个目录中,这也应该是您部署函数的同一个目录。

    另外,google-cloud 包已被弃用,不应与其他google-cloud-* 包一起使用。您应该将其从您的 requirements.txt 文件中删除。

    【讨论】:

    • 无论是否在 requirements.txt 文件中包含 google-cloud 包,我都会遇到相同的错误。
    • 我看到您正在使用 Cloud Source Repositories。您是否确保在将新提交推送到存储库后重新部署该功能?它不会自动部署。
    • 是的,我推送到我的存储库,然后我在终端中运行 gcloud deploy ... 命令,这就是我在函数部署期间收到此错误的时候。
    • 您的存储库中是否有可能包含其他依赖项的lib 文件夹?
    • 不,没有带有其他依赖项的 lib 文件夹。
    【解决方案2】:

    要安装google-cloud库,需要执行

    pip install google-cloud-storage
    

    可以在 Google Cloud 官方文档中看到,所以不要安装google-cloud-pubsub

    尽管如此,你还是像之前那样导入了 pubsub 包

    from google.cloud import pubsub_v1
    

    同样,官方 Google Cloud 文档here 中存在一个专用于 pubsub_v1 库的页面,其中显示了以下示例:

    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)
    

    【讨论】:

      猜你喜欢
      • 2022-01-24
      • 2021-02-06
      • 2021-03-21
      • 1970-01-01
      • 2020-12-29
      • 2021-01-16
      • 2018-12-11
      • 1970-01-01
      • 2021-03-12
      相关资源
      最近更新 更多