在 Google 云函数上部署 python C 包时出错

Error when deploying python C packages on Google Cloud functions

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

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

好像是因为你不能用requirements.txt pip install一个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

我遇到这个问题是因为我在没有 docker 的情况下使用无服务器框架。当前(截至 2019 年 8 月 29 日)a bug 在无服务器-python-要求中阻止 dockerizing pip 与私有存储库。

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

Here is a link 到我的解决方案。