重新部署Google Cloud Function丢失Python依赖问题
Google Cloud Function重部署失败:找不到Git引用的Python包
问题背景
我有一个包含main.py和requirements.txt的Google Cloud Function:
main.py
from superpackage.subpackage import TheSuperClass def watch_function(event, context): print(f"Event ID: {context.event_id}") print(f"Event type: {context.event_type}") print("Bucket: {}".format(event["bucket"])) print("File: {}".format(event["name"])) print("Metageneration: {}".format(event["metageneration"])) print("Created: {}".format(event["timeCreated"])) print("Updated: {}".format(event["updated"])) a = TheSuperClass()
requirements.txt(通过Git仓库引用Python包,本地运行正常):
-e git+https://github.com/someorg/superpackage.git@v0.2#egg=superpackage
首次部署使用以下命令成功,函数能正常响应存储桶文件上传事件:
gcloud functions deploy peter-function \ --runtime python311 \ --trigger-resource the-bucket-we-watch \ --entry-point watch_function \ --trigger-event google.storage.object.finalize
问题现象:修改main.py后,用相同命令重部署同名函数时失败,提示找不到superpackage模块;但改用新函数名部署修改后的代码则正常运行。已将gcloud SDK更新至最新版本。
报错信息:
Deploying function (may take a while - up to 2 minutes)...failed. ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Function failed on loading user code. This is likely due to a bug in the user code. Error message: Traceback (most recent call last): File "/layers/google.python.pip/pip/bin/functions-framework", line 8, in <module> sys.exit(_cli()) ^^^^^^ File "/layers/google.python.pip/pip/lib/python3.11/site-packages/click/core.py", line 1157, in __call__ return self.main(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/layers/google.python.pip/pip/lib/python3.11/site-packages/click/core.py", line 1078, in main rv = self.invoke(ctx) ^^^^^^^^^^^^^^^^ File "/layers/google.python.pip/pip/lib/python3.11/site-packages/click/core.py", line 1434, in invoke return ctx.invoke(self.callback, **ctx.params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/layers/google.python.pip/pip/lib/python3.11/site-packages/click/core.py", line 783, in invoke return __callback(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/layers/google.python.pip/pip/lib/python3.11/site-packages/functions_framework/_cli.py", line 37, in _cli app = create_app(target, source, signature_type) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/layers/google.python.pip/pip/lib/python3.11/site-packages/functions_framework/__init__.py", line 288, in create_app spec.loader.exec_module(source_module) File "<frozen importlib._bootstrap_external>", line 940, in exec_module File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed File "/workspace/main.py", line 1, in <module> from superpackage.subpackage import TheSuperClass ModuleNotFoundError: No module named 'superpackage'. Please visit https://cloud.google.com/functions/docs/troubleshooting for in-depth troubleshooting documentation.
问题原因
这是Google Cloud Function增量部署的缓存机制导致的:重部署同名函数时,系统会复用已有运行环境的缓存,而requirements.txt中使用的-e(可编辑模式)Git包,在缓存复用过程中没有被正确重新加载,最终导致依赖缺失。而使用新函数名部署时会创建全新运行环境,能正常安装所有依赖。
解决方法
1. 部署时禁用缓存
在部署命令中添加--no-cache参数,强制系统重新构建运行环境并安装所有依赖:
gcloud functions deploy peter-function \ --runtime python311 \ --trigger-resource the-bucket-we-watch \ --entry-point watch_function \ --trigger-event google.storage.object.finalize \ --no-cache
2. 修改requirements.txt移除可编辑模式
将requirements.txt中的-e前缀移除,换成普通的Git包引用,避免缓存机制的兼容性问题:
git+https://github.com/someorg/superpackage.git@v0.2#egg=superpackage
修改后执行原部署命令即可。
3. 删除旧函数后重新部署
先删除已存在的函数,再重新部署,确保使用全新的运行环境:
gcloud functions delete peter-function # 执行原部署命令 gcloud functions deploy peter-function \ --runtime python311 \ --trigger-resource the-bucket-we-watch \ --entry-point watch_function \ --trigger-event google.storage.object.finalize
内容的提问来源于stack exchange,提问作者pjotr_dolphin
相关产品推荐
相关产品推荐

