【问题标题】:ImportError: cannot import name 'tasks_v2' from 'google.cloud'ImportError:无法从“google.cloud”导入名称“tasks_v2”
【发布时间】:2021-03-12 17:24:03
【问题描述】:

我正在尝试使用此处 GCP 文档中提供的 Google Cloud 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

【问题讨论】:

    标签: python google-cloud-platform google-cloud-tasks


    【解决方案1】:

    这对我有用:

    pip3 install -U google-cloud-tasks
    

    如果您在为其他 GCP 服务导入包时遇到任何问题,这是一个很好的资源: https://github.com/googleapis/google-cloud-python。链接直接指向pypi.org 上的相关包名称。

    【讨论】:

      【解决方案2】:

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

      对于creating http task,需求文件为this one

      google-cloud-tasks==2.0.0
      

      这是您要查找的确切依赖项和版本

      【讨论】:

        猜你喜欢
        • 2021-02-06
        • 2021-03-21
        • 1970-01-01
        • 2019-02-26
        • 2020-12-29
        • 2021-01-16
        • 1970-01-01
        • 2022-01-22
        • 2021-06-30
        相关资源
        最近更新 更多