Flask API项目无法运行:cached_property导入报错求助
解决方法
方案1:降级Werkzeug到兼容版本
flask-restplus已经停止维护,和新版本Werkzeug存在兼容性问题——新版本Werkzeug把cached_property从根模块移走了。你可以安装一个兼容的旧版本:
pip install werkzeug==2.0.3
完成后重新执行python -m flask run即可正常启动项目。
方案2:替换为维护中的flask-restx
flask-restx是flask-restplus的官方维护分支,完全兼容原有代码,同时修复了依赖适配问题:
- 卸载旧的flask-restplus:
pip uninstall flask-restplus -y
- 安装flask-restx:
pip install flask-restx
- 修改代码中的导入语句,把:
from flask_restplus import Api, Resource, fields
替换为:
from flask_restx import Api, Resource, fields
这个方案不仅能解决当前的导入错误,后续还能获得项目的更新支持。
问题根源
flask-restplus项目已停止更新,而Werkzeug在2.1及以上版本中重构了cached_property的位置,从werkzeug根模块迁移到了werkzeug.utils(也可直接使用Python标准库的functools.cached_property),导致旧版flask-restplus的导入代码失效。
内容的提问来源于stack exchange,提问作者Joe-Tech Digitals
相关产品推荐
相关产品推荐

