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

fpe*_*syn 4 python google-cloud-platform google-cloud-tasks

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

但是,我收到以下错误。

ImportError: cannot import name 'tasks_v2' from 'google.cloud'
Run Code Online (Sandbox Code Playgroud)

代码示例:

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
Run Code Online (Sandbox Code Playgroud)

Lou*_*s C 5

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

对于创建 http 任务,需求文件就是这个

它有

google-cloud-tasks==2.0.0
Run Code Online (Sandbox Code Playgroud)

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