在Google Cloud功能上部署python C包时出错

时间:2019-08-26 19:05:27

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

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

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

这似乎是因为您无法通过Requirements.txt安装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

1 个答案:

答案 0 :(得分:1)

我遇到了这个问题,因为我正在使用没有docker的无服务器框架。当前(截至2019年8月29日),无服务器python要求中的a bug可以防止使用私有存储库对Docker进行点子化。

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

Here is a link解决我的问题。