Harshit
Harshit

Reputation: 203

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

the exact error I get is :

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'

I am trying to import sessions from Flask

Upvotes: 20

Views: 49210

Answers (10)

Andres Mujica
Andres Mujica

Reputation: 23

with the upgrade of werkzeug from version 0.16.0 to 1.0.1, the werkzeug.contrib.cache module has been moved to a standalone package cachelib. For example, to import the RedisCache class, please use the following import: from cachelib.redis import RedisCache.

Upvotes: 0

Kyle Graham
Kyle Graham

Reputation: 1

To elaborate on Alexander P's answer: If you're using this on Werkzeug 0.16.0

from werkzeug.contrib.fixers import ProxyFix

You can replace it with this for Werkzeug 2.2.3

from werkzeug.middleware.proxy_fix import ProxyFix

Upvotes: 0

Alexander P
Alexander P

Reputation: 303

Found that module in the source code. Now it can be imported with the following line

from werkzeug.middleware.proxy_fix import ProxyFix

Upvotes: 1

Jake
Jake

Reputation: 186

For my upgrade (0.15.5 -> 2.2.3) Everything is moved or removed from contrib to other modules. I recomend going to the documentation for your specific version of werkzeug and searching the library you are trying to import. I found mine!

from werkzeug.middleware.profiler import ProfilerMiddleware
app = ProfilerMiddleware(app)

Upvotes: 0

Just_Curious
Just_Curious

Reputation: 51

After downgrading werkzeug:

pip install werkzeug==0.16.0

If you get the following: flask 2.0.2 requires Werkzeug>=2.0, but you have werkzeug 0.16.0 which is incompatible

Consider doing:pip install flask==1.1.1

Upvotes: 0

Stephan Schielke
Stephan Schielke

Reputation: 2843

Either downgrade the version to 0.16.0 or replace werkzeug.contrib.cache with cachelib.

I can highly recommend upgrading the package. The deprecated module werkzeug.contrib is very easy to replace!

Install cachelib and replace all imports from:

from werkzeug.contrib.cache import FileSystemCache

to

from cachelib import FileSystemCache

Upvotes: 6

Asad Khan
Asad Khan

Reputation: 289

You will need to downgrade werkzeug version from 1.0.0 to 0.16.0

This solved the problem for me.

Just run the following commands in your project:

python3 -m pip uninstall werkzeug

and then

python3 -m pip install werkzeug==0.16.0

Upvotes: 9

Ken Roy
Ken Roy

Reputation: 1261

For Python 3.8

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

Upvotes: 0

Zouhair Elhadi
Zouhair Elhadi

Reputation: 106

If you still need deprecated code from werkzeug.contrib, you can downgrade Werkzeug version to less than 1.

pip install Werkzeug<1

Upvotes: 0

Aleksey
Aleksey

Reputation: 803

Werkzeug 1.0.0 has removed deprecated code, including all of werkzeug.contrib. You should use alternative libraries for new projects. werkzeug.contrib.session was extracted to secure-cookie.


If an existing project you're using needs something from contrib, you'll need to downgrade to Werkzeug<1:

pip3 install Werkzeug<1

Upvotes: 23

Related Questions