Google Cloud Tasks ImportError:无法导入名称“resource_pb2”

Posted

技术标签:

【中文标题】Google Cloud Tasks ImportError:无法导入名称“resource_pb2”【英文标题】:Google Cloud Tasks ImportError: cannot import name 'resource_pb2' 【发布时间】:2019-10-15 08:24:06 【问题描述】:

尝试使用 google 提供的 python 示例代码将任务添加到 Google Cloud Tasks 队列。来自谷歌的文档在这里:https://cloud.google.com/tasks/docs/creating-appengine-tasks

当我尝试执行端点时,我收到以下错误:

ImportError:无法导入名称“resource_pb2”

有没有人遇到过类似的问题并且知道如何解决这个问题?

提前感谢您的宝贵时间:)

-内森

我尝试将 google-cloud-tasks、google-api、google-cloud 添加到我的 requirements.txt 并在我的虚拟环境中运行 pip install google-cloud-tasks。我也尝试使用此代码:

从 google.cloud 导入 tasks_v2beta3

结果是一样的

这是google提供的示例代码。我已经向他们发送了一条说明,让他们知道示例代码不起作用。

from google.cloud import tasks_v2
from google.protobuf import timestamp_pb2

# Create a client.
client = tasks_v2.CloudTasksClient()

# TODO(developer): Uncomment these lines and replace with your values.
# project = 'my-project-id'
# queue = 'my-appengine-queue'
# location = 'us-central1'
# payload = 'hello'

# Construct the fully qualified queue name.
parent = client.queue_path(project, location, queue)

# Construct the request body.
task = 
        'app_engine_http_request':   # Specify the type of request.
            'http_method': 'POST',
            'relative_uri': '/example_task_handler'
        

if payload is not None:
    # The API expects a payload of type bytes.
    converted_payload = payload.encode()

    # Add the payload to the request.
    task['app_engine_http_request']['body'] = converted_payload

if in_seconds is not None:
    # Convert "seconds from now" into an rfc3339 datetime string.
    d = datetime.datetime.utcnow() + datetime.timedelta(seconds=in_seconds)

    # Create Timestamp protobuf.
    timestamp = timestamp_pb2.Timestamp()
    timestamp.FromDatetime(d)

    # Add the timestamp to the tasks.
    task['schedule_time'] = timestamp

# Use the client to build and send the task.
response = client.create_task(parent, task)

print('Created task '.format(response.name))
return response

我希望代码将任务添加到 Cloud Tasks 队列,但我得到了以下堆栈跟踪:

Traceback (most recent call last)
File "/home/athena_nsunderman/virtualenv/recharge/lib/python3.5/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/home/athena_nsunderman/virtualenv/recharge/lib/python3.5/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/home/athena_nsunderman/virtualenv/recharge/lib/python3.5/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/athena_nsunderman/virtualenv/recharge/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/home/athena_nsunderman/virtualenv/recharge/lib/python3.5/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/home/athena_nsunderman/virtualenv/recharge/lib/python3.5/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/athena_nsunderman/virtualenv/recharge/lib/python3.5/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/athena_nsunderman/virtualenv/recharge/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/home/athena_nsunderman/virtualenv/recharge/lib/python3.5/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/home/athena_nsunderman/virtualenv/recharge/lib/python3.5/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/athena_nsunderman/posttest-get/main.py", line 134, in TestQueueInsert
from google.cloud import tasks_v2
File "/home/athena_nsunderman/virtualenv/recharge/lib/python3.5/site-packages/google/cloud/tasks_v2/__init__.py", line 19, in <module>
from google.cloud.tasks_v2 import types
File "/home/athena_nsunderman/virtualenv/recharge/lib/python3.5/site-packages/google/cloud/tasks_v2/types.py", line 22, in <module>
from google.cloud.tasks_v2.proto import cloudtasks_pb2
File "/home/athena_nsunderman/virtualenv/recharge/lib/python3.5/site-packages/google/cloud/tasks_v2/proto/cloudtasks_pb2.py", line 18, in <module>
from google.api import resource_pb2 as google_dot_api_dot_resource__pb2
ImportError: cannot import name 'resource_pb2'
The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error.

【问题讨论】:

【参考方案1】:

这对我有用:

pip install -U googleapis-common-protos==1.5.10

【讨论】:

以上是关于Google Cloud Tasks ImportError:无法导入名称“resource_pb2”的主要内容,如果未能解决你的问题,请参考以下文章

Google Cloud Tasks 无法向 Cloud Run 进行身份验证

Google Cloud Tasks 和 Google App Engine Python 3

Google Cloud Tasks 未分派 HTTP 请求

每个任务都无法在 Google Cloud Tasks 上执行

使用 HttpRequest 作为 payload_type 时,Google Cloud Tasks 始终将 HttpMethod 设置为 GET

Google Cloud Tasks HTTP 触发器 - 如何禁用重试