部署Google Cloud Functions遇报错:缺少main.py且未指定GOOGLE_FUNCTION_SOURCE
GCP Cloud Function 部署报错:找不到main.py 文件
问题详情
部署GCP Cloud Function时上传压缩包,触发如下报错:
Step #2 - "build": missing main.py and GOOGLE_FUNCTION_SOURCE not specified. Either create the function in main.py or specify GOOGLE_FUNCTION_SOURCE to point to the file that contains the function
本地文件层级结构:
gcp/ --main.py --requirements.txt
main.py 代码:
import functions_framework @functions_framework.http def hello_http(request): """HTTP Cloud Function. Args: request (flask.Request): The request object. Returns: The response text, or any set of values that can be turned into a Response object using `make_response`. """ request_json = request.get_json(silent=True) request_args = request.args if request_json and 'name' in request_json: name = request_json['name'] elif request_args and 'name' in request_args: name = request_args['name'] else: name = 'World' return 'Hello {}!'.format(name)
requirements.txt 内容:
functions-framework==3.*
解决办法
1. 修正压缩包层级(最常见原因)
GCP要求压缩包解压后,main.py和requirements.txt必须在根目录,不能嵌套在子文件夹里。你现在是把gcp/整个文件夹打包了,导致GCP在压缩包根目录找不到目标文件。
- 操作:进入
gcp文件夹内部,直接选中main.py和requirements.txt两个文件,打包成zip压缩包,再重新上传部署。
2. 手动指定函数参数(可选)
如果确认压缩包层级没问题,仍报错,可以在部署时明确指定文件和入口函数:
- 控制台部署:找到「运行时、构建、连接设置」→ 展开「构建」,设置
GOOGLE_FUNCTION_SOURCE为main.py;再到「运行时」部分,指定函数入口为hello_http。 - 命令行部署:用gcloud命令指定源文件夹和入口:
gcloud functions deploy hello_http --runtime python311 --trigger-http --source ./gcp --entry-point hello_http
3. 检查文件完整性与命名
- 确认
main.py文件名拼写完全正确(GCP对文件名大小写敏感,不能写成Main.py或MAIN.PY)。 - 重新生成压缩包,避免文件损坏,推荐使用标准zip格式打包。
内容的提问来源于stack exchange,提问作者AnkurKing11
相关产品推荐
相关产品推荐

