【发布时间】: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