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

时间:2019-05-29 16:58:45

标签: python google-app-engine google-cloud-tasks

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

当我尝试执行端点时,出现以下错误:

导入错误:无法导入名称“ resource_pb2”

有人看见过类似的问题,并且知道如何解决这个问题吗?

提前感谢您的时间:)

-nathan

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

从google.cloud导入task_v2beta3

结果相同

这是google提供的示例代码。我已向他们发送了一封便条,以告知他们示例代码无效。

ALTER TABLE TestTable
ADD DateInserted DATETIME NOT NULL DEFAULT (GETDATE());

我希望代码将任务添加到“ Cloud Tasks Queue”中,而不是获得此堆栈跟踪:

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

1 个答案:

答案 0 :(得分:3)

这对我有用:

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