ImportError: cannot import name ‘safe_str_cmp’ from ‘werkzeug.security’

We Are Going To Discuss About ImportError: cannot import name ‘safe_str_cmp’ from ‘werkzeug.security’. So lets Start this Python Article.

ImportError: cannot import name ‘safe_str_cmp’ from ‘werkzeug.security’

  1. How to solve ImportError: cannot import name 'safe_str_cmp' from 'werkzeug.security'

    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

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

    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 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

Original Author Oliver Tonnesen Of This Content

Solution 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))

Original Author MarcinKonowalczyk Of This Content

Solution 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.

Original Author Hamza Muazzam Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment