Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: cannot import name 'safe_str_cmp' from 'werkzeug.security'

Any ideas on why I get this error?

My project was working fine. I copied it to an external drive and onto my laptop to work on the road; it worked fine. I copied it back to my desktop and had a load of issues with invalid interpreters etc, so I made a new project and copied just the scripts in, made a new requirements.txt and installed all the packages, but when I run it, I get this error:

Traceback (most recent call last):
  File "E:\Dev\spot_new\flask_blog\run.py", line 1, in <module>
    from flaskblog import app
  File "E:\Dev\spot_new\flask_blog\flaskblog\__init__.py", line 3, in <module>
    from flask_bcrypt import Bcrypt
  File "E:\Dev\spot_new\venv\lib\site-packages\flask_bcrypt.py", line 21, in <module>
    from werkzeug.security import safe_str_cmp
ImportError: cannot import name 'safe_str_cmp' from 'werkzeug.security' (E:\Dev\spot_new\venv\lib\site-packages\werkzeug\security.py)

I've tried uninstalling Python, Anaconda, PyCharm, deleting every reg key and environment variable I can find that looks pythonic, reinstalling all from scratch but still no dice.

like image 209
prosody Avatar asked Aug 30 '25 15:08

prosody


2 Answers

Werkzeug released v2.1.0 today, removing werkzeug.security.safe_str_cmp.

You can probably resolve this issue by pinning Werkzeug~=2.0.0 in your requirements.txt file (or similar).

pip install Werkzeug~=2.0.0

After that it is likely that you will also have an AttributeError related to the jinja package, so if you have it, also run:

pip install jinja2~=3.0.3
like image 187
Oliver Tonnesen Avatar answered Sep 02 '25 11:09

Oliver Tonnesen


This issue can also be fixed by upgrading flask_login.

pip install --upgrade flask_login
like image 40
Fabian Avatar answered Sep 02 '25 12:09

Fabian