When I do Flask run, it shows error : ModuleNotFoundError: No module named 'werkzeug.contrib'. Can anyone help me with this?

When I do Flask run, it shows error : ModuleNotFoundError: No module named 'werkzeug.contrib'. Can anyone help me with this?

我得到的确切错误是:

flask.cli.NoAppException: While importing "application", an ImportError was raised:Traceback      (most recent call last):
File "/home/harshit/.local/lib/python3.6/site-packages/flask/cli.py", line 240, in     locate_app
__import__(module_name)
File "/home/harshit/Documents/project1/application.py", line 18, in <module>
Session(app)
File "/home/harshit/.local/lib/python3.6/site-packages/flask_session/__init__.py", line 54,    in __init__
self.init_app(app)
File "/home/harshit/.local/lib/python3.6/site-packages/flask_session/__init__.py", line 61, in init_app
app.session_interface = self._get_interface(app)
File "/home/harshit/.local/lib/python3.6/site-packages/flask_session/__init__.py", line 93, in _get_interface
config['SESSION_USE_SIGNER'], config['SESSION_PERMANENT'])
File "/home/harshit/.local/lib/python3.6/site-packages/flask_session/sessions.py", line 313, in __init__
from werkzeug.contrib.cache import FileSystemCache
ModuleNotFoundError: No module named 'werkzeug.contrib'

我正在尝试从 Flask 导入会话

如果您仍然需要来自 werkzeug.contrib 的弃用代码,您可以将 Werkzeug 版本降级到低于 1。

pip install Werkzeug<1

Werkzeug 1.0.0 已删除不推荐使用的代码,包括所有 werkzeug.contrib。您应该为新项目使用替代库。 werkzeug.contrib.session 被提取到 secure-cookie


如果您正在使用的现有项目需要 contrib 中的某些内容,您需要降级到 Werkzeug<1:

pip3 install Werkzeug<1

为Python3.8

python3 -m pip uninstall werkzeug
python3 -m pip install werkzeug
python3 -m pip install flask-session

您需要将 werkzeug 版本从 1.0.0 降级到 0.16.0

这解决了我的问题。

只需在您的项目中运行以下命令:

python3 -m pip uninstall werkzeug

然后

python3 -m pip install werkzeug==0.16.0

要么将版本降级到 0.16.0,要么将 werkzeug.contrib.cache 替换为 cachelib

我强烈建议升级软件包。已弃用的模块 werkzeug.contrib 非常容易替换!

安装 cachelib 并替换所有导入自:

from werkzeug.contrib.cache import FileSystemCache

from cachelib import FileSystemCache

降级 werkzeug 后:

pip install werkzeug==0.16.0

如果您得到以下信息: flask 2.0.2 requires Werkzeug>=2.0, but you have werkzeug 0.16.0 which is incompatible

考虑做:pip install flask==1.1.1