ImportError: cannot import name 'url_quote' from 'werkzeug.urls' (Python) — how to fix
Traceback (most recent call last):
File "app.py", line 1, in <module>
from flask import Flask
File ".../site-packages/flask/__init__.py", line 7, in <module>
from .app import Flask as Flask
...
ImportError: cannot import name 'url_quote' from 'werkzeug.urls' (.../site-packages/werkzeug/urls.py)
Quick fix: Upgrade everything together: pip install -U flask werkzeug (plus any Flask extensions you use). If you must stay on an old Flask, pin Werkzeug: pip install "werkzeug<3" and add the pin to requirements.txt. In your own code replace werkzeug.urls.url_quote with urllib.parse.quote.
What it means
Werkzeug 3.0 (released September 2023) removed a set of URL helpers from werkzeug.urls that had been deprecated in 2.3, including url_quote, url_parse, url_encode and url_decode. The standard library's urllib.parse does the same job.
Older Flask releases only declared a minimum Werkzeug version, so a fresh pip install of an old pinned Flask pulls Werkzeug 3 alongside it, and the import fails the moment Flask loads. That's why this usually turns up in old projects, tutorials and Docker images rebuilt after a while, even though nobody changed the code.
Check what you have installed:
pip show flask werkzeug | grep -E "Name|Version"
Cause 1: old Flask with new Werkzeug
The traceback goes through site-packages/flask/....
Fix (recommended): upgrade Flask so it matches Werkzeug 3:
pip install -U flask werkzeug
pip freeze | grep -iE "flask|werkzeug" # then update requirements.txt
Recent Flask versions declare a compatible Werkzeug range, so pip installs a matching pair.
Fix (keep old Flask): pin Werkzeug to the 2.x line:
pip install "werkzeug<3"
# requirements.txt
flask==2.2.5
werkzeug<3
Pin both packages explicitly in requirements.txt. Pinning only Flask is what causes the problem in the first place.
Cause 2: a Flask extension imports the removed helpers
If the traceback ends inside another package (an older Flask-Login, Flask-WTF, or some other extension), that package needs upgrading too:
pip install -U flask-login # or whichever package is named in the traceback
If the extension has no release that supports Werkzeug 3, pin werkzeug<3 as above until it does, or replace the extension.
Cause 3: your own code uses werkzeug.urls
Switch to the standard library:
# before
from werkzeug.urls import url_quote, url_parse, url_encode
# after
from urllib.parse import quote, urlparse, urlencode
quote("a b/c") # 'a%20b/c'
urlparse("https://x.io/p") # ParseResult(...)
urlencode({"q": "a b"}) # 'q=a+b'
For quoting a single URL path segment, quote(value, safe="") also encodes /. To check encoded values by hand, use the URL encoder/decoder.
Prevent it
- Keep a lock file (
pip freeze > requirements.txt, pip-tools or Poetry) so rebuilds install the same versions you tested with. - After upgrading, run
pip checkto list any installed packages whose requirements conflict.
Last updated 2026-12-04.