升级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+
- 添加deadsnakes源:
sudo add-apt-repository ppa:deadsnakes/ppa sudo apt update - 安装Python3.9:
sudo apt install python3.9 python3.9-distutils - 可选:将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。- 添加deadsnakes源:
修改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

