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

When you encounter ImportError cannot import name ‘safe_str_cmp’ from ‘werkzeug.security’ while working with Python, it can be a frustrating experience.

Obviously, this error indicates that there is an issue with importing the safe_str_cmp function from the Werkzeug security module, and it can prevent you from running your Python code.

In this article, we will look for solutions to resolve this issue.

What is Importerror: cannot import name ‘safe_str_cmp’ from ‘werkzeug.security’?

The Importerror: cannot import name ‘safe_str_cmp’ from ‘werkzeug.security‘ is an error that occurs when modules are trying to access safe_str_cmp function, however, it was removed in Werkzeug v2.1.0.

Possible Causes

Possible Causes of “Importerror cannot import name ‘safe_str_cmp’ from ‘werkzeug.security‘” error include the following:

  • Outdated versions of Werkzeug and Flask
  • Incompatible versions of Werkzeug and Flask
  • Issues with virtual environments

How to fix Importerror cannot import name ‘safe_str_cmp’ from ‘werkzeug.security’

Now that we knew the causes and what kind of error Importerror: cannot import name ‘safe_str_cmp’ from ‘werkzeug.security’ is, here are the following solutions you can consider.

Upgrading Werkzeug and Flask

One way to fix this is upgrading the module to the latest version especially if you’re using an outdated version of Werkzeug or Flask.

To do this run the following command to the terminal.

pip install --upgrade werkzeug flask

Definitely, it should upgrade the Werkzeug and Flask to the latest versions.

Checking compatibility of versions

When the error persists even after upgrading to the latest versions of Werkzeug and Flask, you may need to check the compatibility of the versions you’re using.

You can do this by checking the release notes of the respective libraries to ensure that they are compatible with each other.

Updating virtual environment

If you’re using a virtual environment, updating the virtual environment may resolve the issue.

You can do this by running the following commands in your terminal.

deactivate
pip install --upgrade virtualenv
virtualenv venv
source venv/bin/activate
pip install --upgrade werkzeug flask

This will deactivate the current virtual environment, upgrade virtualenv, create a new virtual environment, activate the new virtual environment, and install the latest versions of Werkzeug and Flask.

Pinning Werkzeug to Version 2.0.0

Another solution to the ImportError message is to pin Werkzeug to version 2.0.0. you can do this by using the given command:

pip install Werkzeug~=2.0.0

This solution is advised for users who are not using other modules that require Werkzeug v2.1.0 or higher.

Werkzeug v2.1.0 removed the safe_str_cmp function, which caused this ImportError message.

By pinning Werkzeug to version 2.0.0, you can ensure that you’re using a version of Werkzeug that still includes the safe_str_cmp function.

However, this solution may not be suitable for all users, especially if you’re using other modules that require Werkzeug v2.1.0 or higher.

Use Alternative Modules

Another solution to the ImportError message is to use the hashlib and hmac modules instead of Werkzeug.security.

These modules provide similar functionality as the safe_str_cmp function and are compatible with Werkzeug v2.1.0 and higher.

To use these alternative modules, you can replace any instances of Werkzeug.security with hashlib and hmac.

However, this solution may require some code changes and may not be suitable for all users, especially if you’re using other modules that rely on Werkzeug.security.

Anyway, here are other fixed errors you can consider when somehow you might encounter them.

Conclusion

The “cannot import name ‘safe_str_cmp’ from ‘werkzeug.security‘” error can be frustrating to deal with, but it’s important to resolve it in order to ensure that your Python code is running smoothly and securely.

By understanding the causes of the error and using the solutions we’ve provided, you should be able to resolve the issue and continue working on your Python projects with confidence.

I hope this article has helped you fix the error.

Until next time! 😊

Frequently Asked Questions

What is Python ImportError and what causes it?

ImportError is raised when an import fails for any reason. The most specific subtype is ModuleNotFoundError (no such module). Plain ImportError typically means the module exists but a name inside it can’t be imported, e.g. ‘cannot import name X from Y’ (X was renamed, removed, or moved between versions of Y). Common with library version mismatches.

How do I fix ‘cannot import name X from Y’?

Three steps: (1) Check the library version: pip show Y. (2) Check the changelog of Y, X may have been renamed or removed in a recent release. (3) Either pin to an older Y version (pip install Y==1.x.y) or update your code to the new import path. Common 2025-2026 examples: Werkzeug url_decode removed, Pillow ANTIALIAS renamed to LANCZOS.

Why does the import work in REPL but fail in script?

Two reasons. (1) Different Python interpreter: REPL uses one Python, your script uses another. Run python –version both times. (2) Different working directory: REPL is started where you have access to local modules, script is run from a different cwd. Add the project path to sys.path or use python -m to run as a module.

How do I avoid circular import errors?

Circular imports happen when module A imports B and B imports A at the top level. Three fixes: (1) Move one import inside the function that uses it (lazy import). (2) Restructure code so A and B both import from a third module C. (3) Use TYPE_CHECKING for type-hint-only imports: if TYPE_CHECKING: from a import X.

Where can I find more ImportError fixes?

Browse the ImportError reference hub for 67+ specific fixes (Flask, Werkzeug, Django, ML library versions). For missing-module cases see ModuleNotFoundError. For Python setup help see Python Tutorial hub.