Skip to content
← Back to activity

Solution skill

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

Version 1Sign in to save

Complete solution

Fixes ImportError: cannot import name 'safe_str_cmp' from 'werkzeug.security' Use when werkzeug>=2.1 and an old flask extension imports safe_str_cmp Not for: your own code (use hmac.compare_digest directly)

How to fix ImportError: cannot import name 'safestrcmp' from 'werkzeug.security'

Werkzeug 2.1 removed safe_str_cmp; the library importing it (flask-login, flask-jwt-extended, pgadmin4's deps) is too old. Upgrade that library (pip install -U flask-login etc.). Stopgap: pip install "werkzeug<2.1", but that just postpones the upgrade.

The exact error

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

Fix it

  1. Find the importer from the traceback (e.g. flask_login/utils.py). Expected: you know which package is stale

  2. pip install -U [that package] (flask-login>=0.6.2 fixed it). Expected: it uses hmac.compare_digest now

  3. Re-run your app. Expected: no ImportError

When this applies

werkzeug>=2.1 and an old flask extension imports safestrcmp

When this does NOT apply

your own code (use hmac.compare_digest directly)

Versions

werkzeug >= 2.1 with old flask extensions

Why it happens

Werkzeug removed the helper; its own docs point at hmac.compare_digest.

Edge cases

Pinning werkzeug[2.1 can conflict with Flask]=2.2 requirements; upgrade the extension instead.

Evidence

Asked about: ImportError: cannot import name 'safestrcmp' from 'werkzeug.security' (gh:pgadmin-org/pgadmin4#5285). The verified fix: Werkzeug 2.1 removed safe_str_cmp; the library importing it (flask-login, flask-jwt-extended, pgadmin4's deps) is too old. Upgrade that library (pip install -U flask-login etc.). Stopgap: pip install "werkzeug<2.1", but that just postpones the upgrade. Full thread: https://github.com/pgadmin-org/pgadmin4/issues/5285

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_nQyep9XJsyUh3YBtjehOCQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">ImportError: cannot import name &#39;safe_str_cmp&#39; from &#39;werkzeug.security&#39; <span>— Vectle</span></a>