【问题标题】:Dealing with ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Function failed on loading user code处理错误:(gcloud.functions.deploy) OperationError: code=3, message=Function failed on loading user code
【发布时间】:2023-11-25 21:43:01
【问题描述】:

我有这个代码:

from google.cloud import spanner



def search_topic2(request):
    return 2

我正在编译它 gcloud.cmd 函数部署 search_topic2 --trigger-http --runtime python39

非常简单的例子 我得到了
错误: (gcloud.functions.deploy) OperationError: code=3, message=Function failed on loading user code。这可能是由于用户代码中的错误。错误消息:错误:请检查您的函数日志以查看错误原因:https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs。可以在https://cloud.google.com/functions/docs/troubleshooting#logging 找到其他故障排除文档。请访问https://cloud.google.com/functions/docs/troubleshooting 获取深入的故障排除文档。
我知道这是由“来自 google.cloud 导入扳手”引起的...... 知道具体是什么问题以及如何解决吗?

【问题讨论】:

    标签: python google-cloud-spanner


    【解决方案1】:

    您必须将所有要在代码中使用的依赖项/包放在名为requirements.txt 的文件中。如果您需要的包在那里可用,它将准备好导入。在设置环境云功能时,会在后端安装requirements.txt 中提到的所有包。以下是示例requirements.txt

    google-api-core==2.0.1
    google-auth==2.1.0
    google-auth-oauthlib
    google-cloud-bigquery==2.26.0
    google-cloud-core>=2.0.0
    google-crc32c
    

    版本信息是可选的

    您可以手动编写或使用pip freeze>requirements.txt获取它(以防您从本地环境转移到云端功能)

    【讨论】: