Dealing with ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Function failed on loading user code

Dealing with ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Function failed on loading user code

我有这个代码:

from google.cloud import spanner



def search_topic2(request):
    return 2

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

非常简单的例子 我得到
错误:(gcloud.functions.deploy)操作错误:代码=3,消息=函数加载用户代码失败。这可能是由于用户代码中的错误。错误消息:错误:请检查您的函数日志以查看错误原因:https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging. Please visit https://cloud.google.com/functions/docs/troubleshooting 以获得深入的故障排除文档。
我知道这是由“from google.cloud import spanner”引起的...... 知道问题到底是什么以及如何解决吗?

您必须将所有要在代码中使用的 dependencies/packages 放入名为 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获取它(以防您从本地环境转移到云功能)