【问题标题】:Error when deploying python C packages on Google Cloud functions在 Google Cloud 函数上部署 python C 包时出错
【发布时间】:2019-12-30 23:28:18
【问题描述】:

我正在使用 serverless-google-cloudfunctions 使用 python37 部署 Google Cloud 功能。此函数使用 pubsub API 发布消息。但是,我收到以下错误:

ImportError: cannot import name 'cygrpc' from 'grpc._cython'

这似乎是因为您无法使用 requirements.txt pip 安装 C 库。我该如何解决这个问题?我的代码如下。

from google.cloud import pubsub

publisher = pubsub.PublisherClient()
path = publisher.topic_path("my_proj", "my_topic")

publisher.publish(path, "test".encode("utf-8"))

我的 requirements.txt 如下。我尝试添加grpcio==1.22.0 无济于事。

google-cloud-pubsub==0.45.0

我的 serverless.yml:

service: my-service

provider:
    name: google
    stage: ${opt:stage, 'dev'}
    runtime: python37
    region: us-central1
    project: ${self:custom.env.PROJECT_NAME}
    credentials: ~/.gcloud/keyfile.json

plugins:
  - serverless-google-cloudfunctions
  - serverless-python-requirements

custom:
    pythonRequirements:
        fileName: private_requirements.txt
        pythonBin: python3
        noDeploy:
            - requirements.txt
    stage:
        ${self:provider.stage}
    env:
        ${file(./.env.${self:provider.stage})}

package:
    include:
        - requirements.txt
    exclude:
        - .git/**
        - .gitignore
        - env*
        - node_modules/**
        - package.json
        - private_requirements.txt
        - yarn.lock

functions:

    my-func:
        handler: func
        events:
            - http: path

【问题讨论】:

  • 您的requirements.txt 文件内容是什么?你的项目结构是什么?如何部署你的函数?
  • Requirements.txt 在上面列出。项目结构就是根文件夹下的 requirements.txt、serverless.yml、main.py、package.json。我使用serverless deploy 进行部署
  • 您可以尝试使用 gcloud 命令进行部署吗?目的是确定问题是来自您的代码还是来自部署管理器。
  • 我只是尝试使用您的代码在控制台中部署一个函数,它对我有用。我添加了两个依赖项 grpcio 和 pubsub,没问题。
  • 我添加了 serverless.yml。重要的部分是我从预部署包安装中排除了 requirements.txt,然后在部署包中包含了需求文件。这是因为 GCP 会在上传包时安装 requirements.txt,而我必须在上传包之前使用我的 git 凭据安装我的私有需求。

标签: python-3.x google-cloud-platform google-cloud-functions serverless-framework google-cloud-pubsub


【解决方案1】:

我遇到了这个问题,因为我使用的是没有 docker 的无服务器框架。目前(截至 2019 年 8 月 29 日)a bug 在 serverless-python-requirements 中阻止使用私有存储库对 pip 进行 dockerizing。

我的解决方案是删除无服务器并转换为 gcloud CLI。当您将 requirements.txt 上传到 GCloud 时,它会自动安装公共存储库,但无法安装私有存储库,因为它没有 git 凭据。为了解决这个问题,您必须在将包上传到 gcloud 之前在本地安装这些要求。

Here is a link 我的解决方案。

【讨论】:

    猜你喜欢
    • 2020-03-01
    • 1970-01-01
    • 2019-09-23
    • 2020-01-19
    • 2020-06-03
    • 1970-01-01
    • 1970-01-01
    • 2019-04-24
    • 2020-11-14
    相关资源
    最近更新 更多