pythonflaskwerkzeug

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.


Solution

  • 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