Azure-Samples / ms-identity-python-webapp

A Python web application calling Microsoft graph that is secured using the Microsoft identity platform
MIT License
291 stars 138 forks source link

ModuleNotFoundError: No module named 'werkzeug.contrib' #16

Closed mrhoads closed 4 years ago

mrhoads commented 4 years ago

I followed the steps in the tutorial; however, when attempting to run python app.py or flask run --port 5000I received:

Traceback (most recent call last):
File "app.py", line 11, in <module>
Session(app)
  File "/home/mrhoads/development/ms-identity-python-webapp/venv/lib/python3.7/site-packages/flask_session/__init__.py", line 54, in __init__
self.init_app(app)
  File "/home/mrhoads/development/ms-identity-python-webapp/venv/lib/python3.7/site-packages/flask_session/__init__.py", line 61, in init_app
app.session_interface = self._get_interface(app)
  File "/home/mrhoads/development/ms-identity-python-webapp/venv/lib/python3.7/site-packages/flask_session/__init__.py", line 93, in _get_interface
config['SESSION_USE_SIGNER'], config['SESSION_PERMANENT'])
  File "/home/mrhoads/development/ms-identity-python-webapp/venv/lib/python3.7/site-packages/flask_session/sessions.py", line 313, in __init__
from werkzeug.contrib.cache import FileSystemCache
ModuleNotFoundError: No module named 'werkzeug.contrib'

I seemed to work around it by running pip3 uninstall Werkzeug followed by pip3 install Werkzeug==0.16.0. That gets the app to run, though I don't know specifically why. I wanted to capture it in an issue and the workaround for others who might encounter this.

rayluo commented 4 years ago

The first search result of that error message ModuleNotFoundError: No module named 'werkzeug.contrib' leads me to this post created just 4 days ago. There are some discussion there too, speculating Flask issue or environment issue.

@mrhoads What version of these packages are you using?

CC @navyasric, @abhidnya13: Perhaps we can try and see if we can reproduce this in a clean environment. @abhidnya13 did you observe same issue during our tests in recent days? If not, there would be not much we can do here.

mrhoads commented 4 years ago
In a new virtualenv installing the requirements.txt I see: Package Version
certifi 2019.11.28
cffi 1.14.0
chardet 3.0.4
Click 7.0
cryptography 2.8
Flask 1.1.1
Flask-Session 0.3.1
idna 2.8
itsdangerous 1.1.0
Jinja2 2.11.1
MarkupSafe 1.1.1
msal 1.1.0
pip 20.0.2
pycparser 2.19
PyJWT 1.7.1
requests 2.22.0
setuptools 45.2.0
six 1.14.0
urllib3 1.25.8
Werkzeug 1.0.0
wheel 0.34.2
daemonblade commented 4 years ago

Werkzeug 1.0.0 has removed deprecated code from 'werkzeug.contrib'. You can downgrade to Werkzeug 0.16.0 to work around this issue.

rayluo commented 4 years ago

Thanks @daemonblade for the info! So this issue should be reported to our dependency, flask_session, and, it happens to already have a duplicated issue there. Ideally this should also be fixed from there. We will close this issue here, soon.

JohannesSchwegler commented 4 years ago

@daemonblade downgrade works, thank 👍

Frost199 commented 4 years ago

I ran into the same issue when trying to use Proxyfix in werkzeug==1.0.0

After downgrading to werkzeug==0.16.0, I got these warnings:

To fix the issues:

For ProxyFix:

For FileSystemCache, you will have to install pallets:

I hope this helps and fix your issues 😄

realJustinLee commented 4 years ago

@Frost199 Dude, you are a life saver!

donnguyen281 commented 4 years ago

I am new to Linux and all the command lines that comes with it but am willing to learn!! Can you tell me what I am doing wrong here:

health@T3500:~/gnuhealth/tryton/server/trytond-5.0.18/bin $ python3 ./trytond-admin --all --database=health01 Traceback (most recent call last): File "./trytond-admin", line 21, in admin.run(options) File "/home/health/gnuhealth/tryton/server/trytond-5.0.18/trytond/admin.py", line 54, in run activatedeps=options.activatedeps) File "/home/health/gnuhealth/tryton/server/trytond-5.0.18/trytond/pool.py", line 149, in init self.start() File "/home/health/gnuhealth/tryton/server/trytond-5.0.18/trytond/pool.py", line 102, in start register_classes() File "/home/health/gnuhealth/tryton/server/trytond-5.0.18/trytond/modules/init.py", line 318, in register_classes import trytond.res File "/home/health/gnuhealth/tryton/server/trytond-5.0.18/trytond/res/init.py", line 8, in from . import routes File "/home/health/gnuhealth/tryton/server/trytond-5.0.18/trytond/res/routes.py", line 10, in from trytond.wsgi import app File "/home/health/gnuhealth/tryton/server/trytond-5.0.18/trytond/wsgi.py", line 12, in from werkzeug.contrib.fixers import ProxyFix ModuleNotFoundError: No module named 'werkzeug.contrib'

rayluo commented 4 years ago

Thanks @Frost199 for the hint! With such root cause identified, we believe this issue in upstream repo will probably be fixed by this PR there.

willeswa commented 4 years ago

Just in case someone comes around before the fix PR is merged, downgrade to version 0.6.1

donnguyen281 commented 4 years ago

I tried removing werkzeug version 1.0,.0 and installed werkzeug version 0.16.0 Typed in command ./trytond-admin --all --database=health01 = No output of what's going on and did not give me the option to add a password, Doing something in the background and just ended....

I will try to downgrade to version 0.6.1 and will let you guys know. Newbie to LINUX and Programming. THANK YOU FOR YOUR HELP!!!!!!!!

Thanks guys!!!!

ZayRTun commented 4 years ago

I am having this issue too.

File "/home/zay/Python/tasks/venv/lib/python3.6/site-packages/Flask_Session-0.3.1-py3.6.egg/flask_session/sessions.py", line 313, in init from werkzeug.contrib.cache import FileSystemCache ModuleNotFoundError: No module named 'werkzeug.contrib'

pip list below click (7.1.1) Flask (1.1.1) Flask-Session (0.3.1) itsdangerous (1.1.0) Jinja2 (2.11.1) MarkupSafe (1.1.1) pip (9.0.1) pkg-resources (0.0.0) setuptools (39.0.1) Werkzeug (1.0.0)

Alshafei2430 commented 4 years ago

I ran into the same issue when trying to use Proxyfix in werkzeug==1.0.0

After downgrading to werkzeug==0.16.0, I got these warnings:

  • DeprecationWarning: 'werkzeug.contrib.fixers.ProxyFix' has moved to 'werkzeug.middleware.proxy_fix.ProxyFix'. This import is deprecated as of version 0.15 and will be removed in 1.0.

  • DeprecationWarning: 'werkzeug.contrib.cache' is deprecated as of version 0.15 and will be removed in version 1.0. It has moved to pallets. from werkzeug.contrib.cache import FileSystemCache

To fix the issues:

  • pip install werkzeug==1.0.0

For ProxyFix:

  • from werkzeug.middleware.proxy_fix import ProxyFix

For FileSystemCache, you will have to install pallets:

  • pip install -U cachelib
  • from cachelib.file import FileSystemCache

I hope this helps and fix your issues 😄

Thank you, this solution worked for me . I was struggling in that problem all for two days, thank you again.

rayluo commented 4 years ago

@Frost199 , would you mind contact me and share how exactly you workaround this?

In particular, how did you use that from cachelib.file import FileSystemCache? Did you have to patch that flask_session too?

Alshafei2430 commented 4 years ago

After installing cachelib go to site-packages and change the session.py file
change "from werkzeug.contrib.cache import FileSystemCache" in line 313 to "from cachelib.file import FileSystemCache" image

filak commented 4 years ago

@Alshafei2430 Thanks for the tip, but it did not work for me - the cachlib seems to be different...

I use Flask-Caching so I have used this import instead:

from flask_caching.backends.filesystem import FileSystemCache

rayluo commented 4 years ago

Hi @filak , I also followed @Alshafei2430 's hint, and it worked, at least on my computerTM. :-)

Would you mind following step 2 of this pending PR to see if that workaround would work for you too? It would be helpful to have more people testing it, before we merge it in.

filak commented 4 years ago

@rayluo I do not have the Azure tenant and it seems a bit complicated...

The cachelib FileSystemCache do not play well with Flask-session - it keeps resetting the session so I was not able to login. When using the module from flask_caching everything works as before.

abhidnya13 commented 4 years ago

@rayluo Same happens when I test it too as @filak

madsjordt commented 4 years ago

Hi guys, when executing "flask run" I receive the following error message: Error: While importing "application", an ImportError was raised:

Traceback (most recent call last): File "/home/ubuntu/.local/lib/python3.7/site-packages/flask/cli.py", line 240, in locate_app import(module_name) File "/home/ubuntu/pset8/finance/application.py", line 9, in from werkzeug.contrib.cache import FileSystemCache ModuleNotFoundError: No module named 'werkzeug.contrib'

I tried the hints provided in this thread but it just won't work. Quite sure I am doing something wrong but I just don't know what. Hope one of you guys can help me!

Frost199 commented 4 years ago

a lot can contribute to this, can you share your requirements.txt so we know the libraries that are dependent on werkzeug, it might be flask uploads, it might be something else, so we can not tell where the problem is from if this is all the information you are giving.

madsjordt commented 4 years ago

Of course @Frost199, totally right. Please find requirements.txt content attached.

Bildschirmfoto 2020-04-08 um 16 53 47 Bildschirmfoto 2020-04-08 um 16 53 41 Bildschirmfoto 2020-04-08 um 16 53 30

Frost199 commented 4 years ago

from your attachment, you are using werkzeug==0.16.0, you shouldn't have such issues using with werkzeug.contrib as the first issue made by @mrhoads on this thread was fixed downgrading to 0.16.0, you might be doing something wrong and this error is only thrown if you are using werkzeug>=1.0

here is a screenshot from werkzeug changelog 1.0.x Screenshot 2020-04-08 at 16 20 16

or check out the link werkzeug changelog 1.0.x

gottigjavier commented 4 years ago

First (pip or pip3): $ pip3 install werkzeug==1.0.0 $ pip3 install -U cachelib

Then, you need change in flask_session/sessions.py, around line 311: from werkzeug.contrib.cache import FileSystemCache for: from cachelib import FileSystemCache

rayluo commented 4 years ago

@gottigjavier I've fixed this via #21.

LoveDuckie commented 4 years ago

Hey everyone.

I found that the easiest way of dealing with this problem is by simply copying the contents of the FileSystemSessionInterface, making a new version inside of your own project, and changing from werkzeug.contrib.cache import FileSystemCache to from cachelib.file import FileSystemCache.

I don't agree with the approach of modifying the contents of a package once it has been installed with pip install. The reason being is that if you are deploying your Flask application to a platform such as Google App Engine, you won't be able to inject the aforementioned modifications once the runtime has installed all of your service's dependencies from the requirements.txt file.

Hopefully someone takes over this project or fixes the problem soon, but in the mean time, I found that was the best method for fixing this (if you're reliant on the filesystem implementation)!

Many thanks to @Frost199 for identifying where the missing reference now exists. :)

rayluo commented 4 years ago

Hi @LoveDuckie , isn't this even easier?

pip uninstall flask-session
git+https://github.com/rayluo/flask-session@0.3.x#egg=flask-session

Specifically, it does not "modifying the contents of a package once it has been installed with pip install". It just installs a workable version, technically still via pip install.

LoveDuckie commented 4 years ago

@rayluo is git+https://github.com/rayluo/flask-session@0.3.x#egg=flask-session definable as a dependency under a generated requirements.txt?

For example, can that be placed under all the other listings in a requirements.txt file?

rayluo commented 4 years ago

@LoveDuckie , you mean, like this? :-)

rayluo commented 4 years ago

@LoveDuckie , you may not realize this. There are actually 2 different packages here:

I'm one of the contributor of this web app sample, but I do NOT own that flask-session. The best I can do, for now, is to provide a fix, hosted in my fork repo, and then have this web app sample to "use a github user's fork" - and that user happens to be me.

Feel free to make an educated decision on choosing whatever path you prefer.

LoveDuckie commented 4 years ago

I misunderstood that this issue was being raised on a completely unrelated sample code repository that you are a contributor of, and not the flask-session repository. Regardless of good intentions I don't think it's a good idea to use someone else's GitHub fork as a dependency on a production service, irrespective of who that fork belongs to.

Good luck with taking over flask-session. Thanks again for your efforts.

ednasawe commented 4 years ago

Thank you, downgrading to Werkzeug 0.16.0 from 1.0.0 worked!

b-hintz commented 4 years ago

Thank you so much for providing this answer! I was tearing my hair out over this issue!

Divide-By-0 commented 4 years ago

In case anyone else is having this error, I changed from werkzeug.contrib.profiler import ProfilerMiddleware to from werkzeug.middleware.profiler import ProfilerMiddleware

rayluo commented 4 years ago

@Divide-By-0 , I suspect that you were brought to this thread by search engine based on keywords. But this issue and this repo are for a Web App sample for Microsoft identity platform, and we've fixed that issue in our sample 4 months ago.

I'll probably lock this conversation thread, to avoid future confusion on both sides.