'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 1:[1]

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, run:

pip install jinja2==3.0.3

Solution 2:[2]

Werkzeug 2.1.0 release notes recommend using the hmap equivalent. For reference, here is the implementation of safe_str_cmp from wekzeug 2.0.x, and here is a stripped-down version:

import hmac

def safe_str_cmp(a: str, b: str) -> bool:
    """This function compares strings in somewhat constant time. This
    requires that the length of at least one string is known in advance.

    Returns `True` if the two strings are equal, or `False` if they are not.
    """

    if isinstance(a, str):
        a = a.encode("utf-8")  # type: ignore

    if isinstance(b, str):
        b = b.encode("utf-8")  # type: ignore

    return hmac.compare_digest(a, b)

or even more stripped-down one:

import hmac
str_to_bytes = lambda s: s.encode("utf-8") if isinstance(s, str) else s
safe_str_cmp = lambda a, b: hmac.compare_digest(str_to_bytes(a), str_to_bytes(b))

Solution 3:[3]

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

To Solve ImportError: cannot import name 'safe_str_cmp' from 'werkzeug.security' Error You can also

Downgrade Werkzeug to 2.0.0

is working fine So you can Just downgrade Werkzeug to 2.0.0 just run this command:

pip install Werkzeug==2.0.0

OR

pip install Werkzeug==2.1.0

now your error must be solved.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 The Dan
Solution 2 MarcinKonowalczyk
Solution 3 Hamza Muazzam