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

升级gcloud组件后运行dev_appserver.py遇functools.cache属性错误

问题原因

*functools.cache*是Python 3.9及以上版本才新增的装饰器,而Ubuntu 20.04.6 LTS默认预装的Python版本为3.8.x,Google Cloud CLI 472.0.0版本的dev_appserver.py依赖了这个高版本Python特性,导致在Python3.8环境下运行时抛出AttributeError: module 'functools' has no attribute 'cache'错误。

解决方案
  • 升级系统Python版本到3.9+

    1. 添加deadsnakes源:
      sudo add-apt-repository ppa:deadsnakes/ppa
      sudo apt update
      
    2. 安装Python3.9:
      sudo apt install python3.9 python3.9-distutils
      
    3. 可选:将Python3.9设为默认(避免影响其他依赖Python3.8的程序):
      sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
      sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2
      sudo update-alternatives --config python3
      

    完成后重新升级Google Cloud CLI到472.0.0版本,即可正常运行dev_appserver.py。

  • 修改GCloud相关文件兼容Python3.8
    找到报错文件/home/ml/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/context/__init__.py,将第37行的@functools.cache替换为兼容Python3.8的写法:

    @functools.lru_cache(maxsize=None)
    

    functools.cache本质是functools.lru_cache(maxsize=None)的别名,替换后无需升级Python即可使用新版GCloud CLI。

  • 指定dev_appserver.py使用Python3.9运行
    若不想修改系统默认Python版本,可直接用Python3.9执行脚本:

    python3.9 /home/ml/google-cloud-sdk/platform/google_appengine/dev_appserver.py [你的应用参数]
    
  • 固定Google Cloud CLI到兼容版本
    若暂时不想升级Python或修改文件,可继续使用435.0.1版本,执行以下命令锁定版本:

    gcloud components update --version=435.0.1
    

    后续升级前需确认新版本是否兼容Python3.8环境。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 10:17:25