You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

第二代GCF部署依赖报错:无法导入resourcemanager_v3

部署第二代Google Cloud Functions时的依赖导入错误问题

问题详情

我在部署第二代Google Cloud Functions(GCF)时遇到依赖导入错误,具体情况如下:

我的requirements.txt文件内容:

google-cloud-secret-manager>=2.16.1
google-cloud-resource-manager>=1.10.1

按照官方指导,我用以下命令下载依赖到指定目录:

pip download
  -d /user/projects/function/deps/ 
  -r /user/projects/function/src/requirements.txt 
  --platform manylinux2014_x86_64 
  --python-version 311 
  --only-binary=":all:" 
  --implementation cp

当前文件结构:

/user/
└──project/
    ├── function/
       ├── deps/
          └── ... 下载好的*.whl依赖包
       ├── src/
          ├── requirements.txt
          └── 其他Python文件
       └── main.py

执行部署命令:

gcloud functions deploy "my-demo-function" \
    --gen2 \
    --region=region \
    --project=project \
    --runtime=python311 \
    --timeout=540 \
    --trigger-topic=my-demo-topic \
    --entry-point=process_event \
    --set-env-vars=GOOGLE_VENDOR_PIP_DEPENDENCIES=deps \
    --source=/user/projects/function/

部署时出现错误:

ImportError: cannot import name 'resourcemanager_v3' from 'google.cloud' (unknown location)

我尝试将GOOGLE_VENDOR_PIP_DEPENDENCIES设置为deps和/workspace/deps,都无法解决问题。


解决方法

1. 升级google-cloud-resource-manager版本

错误的核心原因是你指定的google-cloud-resource-manager>=1.10.1版本不包含resourcemanager_v3模块——这个模块是在2.x版本的库中才引入的。

修改requirements.txt,将依赖版本更新为支持v3 API的版本:

google-cloud-secret-manager>=2.16.1
google-cloud-resource-manager>=2.0.0

2. 重新下载依赖

确保使用更新后的requirements.txt重新下载依赖,注意路径要和你的文件结构一致(避免路径拼写错误):

pip download
  -d /user/project/function/deps/ 
  -r /user/project/function/src/requirements.txt 
  --platform manylinux2014_x86_64 
  --python-version 311 
  --only-binary=":all:" 
  --implementation cp

3. 验证依赖包

检查deps目录下是否存在google_cloud_resource_manager-2.x.x-py2.py3-none-any.whl(x为具体版本号)这类文件,确认新版本依赖已正确下载。

4. 重新部署

保持原有部署命令不变(GOOGLE_VENDOR_PIP_DEPENDENCIES=deps是正确的,因为它是相对于部署源目录/user/projects/function/的路径),重新执行部署命令即可。

备选方案:放弃手动依赖打包,让GCF自动安装

如果手动打包依赖仍然有问题,可以简化流程:

  1. 将requirements.txt从src目录移动到function目录下(和main.py同级)
  2. 部署命令中移除--set-env-vars=GOOGLE_VENDOR_PIP_DEPENDENCIES=deps参数
  3. 重新执行部署,GCF会自动根据根目录下的requirements.txt安装所需依赖,无需手动下载wheel包。

内容的提问来源于stack exchange,提问作者mateusz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 09:33:10