背景


启动Flask项目时存在如下报错:

1
2
3
4
5
C:\Users\lzy\AppData\Local\Programs\Python\Python38\python.exe D:/Python/FlaskDemo/main.py
Traceback (most recent call last):
File "D:/Python/FlaskDemo/main.py", line 1, in <module>
...
ImportError: cannot import name 'cached_property' from 'werkzeug' (C:\Users\lzy\AppData\Local\Programs\Python\Python38\lib\site-packages\werkzeug\__init__.py) ...

解决


1、显示导入

原因:是因为werkzeug新版本里需要显式导入该模块。

在如下文件中加入:

werkzeug.png

1
from werkzeug.utils import cached_property

如果不使用pycharm,则在python安装目录的site-packages/werkzeug/__init__.py 文件中,添加上述代码。

2、降低werkzeug版本

安装0.16.1版本的werkzeug:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
PS C:\Users\lzy> pip uninstall Werkzeug
Found existing installation: Werkzeug 1.0.1
Uninstalling Werkzeug-1.0.1:
Would remove:
c:\users\lzy\appdata\local\programs\python\python38\lib\site-packages\werkzeug-1.0.1.dist-info\*
c:\users\lzy\appdata\local\programs\python\python38\lib\site-packages\werkzeug\*
Proceed (y/n)? y
Successfully uninstalled Werkzeug-1.0.1

PS C:\Users\lzy> pip install Werkzeug==0.16.1
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting Werkzeug==0.16.1
Downloading https://mirrors.aliyun.com/pypi/packages/c2/e4/a859d2fe516f466642fa5c6054fd9646271f9da26b0cac0d2f37fc858c8f/Werkzeug-0.16.1-py2.py3-none-any.whl (327 kB)
|████████████████████████████████| 327 kB 819 kB/s
Installing collected packages: Werkzeug
Successfully installed Werkzeug-0.16.1