ImportError:无法从“google.cloud”导入名称“tasks_v2”

2024-03-16

我正在尝试使用 GCP 文档中提供的 Google Cloud Tasks 代码示例:https://cloud.google.com/tasks/docs/creating-http-target-tasks https://cloud.google.com/tasks/docs/creating-http-target-tasks(参见下面的代码)。但是,从文档中尚不清楚我需要哪个包才能使其工作。有任何想法吗?

但是,我收到以下错误。

ImportError: cannot import name 'tasks_v2' from 'google.cloud'

代码示例:

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

当遵循 GCP 文档上的任何指南时,请转到 github 存储库并搜索要求.txt文件,这里将是使用的所有依赖项以及版本。

For the 创建http任务 https://cloud.google.com/tasks/docs/creating-http-target-tasks需求文件是this one https://github.com/googleapis/python-tasks/blob/master/samples/snippets/requirements.txt.

It has

google-cloud-tasks==2.0.0

这是您正在寻找的确切依赖项和版本

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ImportError:无法从“google.cloud”导入名称“tasks_v2” 的相关文章

随机推荐