Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Flask crashes on start with cannot import name url_quote from werkzeug.urls

Your error

ImportError: cannot import name 'url_quote' from 'werkzeug.urls'2 more lines it can show

Fix it fast

Most common cause, 1 of 3

An old Flask release was installed next to Werkzeug 3

Move to a current Flask; 2.2.4 was the first release updated for Werkzeug 2.3, and Flask 3.1.3 needs Werkzeug 3.1 or later.

Fix
# preferred
python -m pip install --upgrade "flask==3.1.3" "werkzeug==3.1.9"
# stop-gap for an app stuck on Flask 2.2.3 or older
python -m pip install "werkzeug<3"
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

ImportError: cannot import name 'url_quote' from 'werkzeug.urls' means a Flask release older than 2.2.4 met Werkzeug 3, which removed most of werkzeug.urls. Old Flask allows any newer Werkzeug, so a fresh install breaks. Upgrade to Flask 3.1.3 with Werkzeug 3.1.9, or pin werkzeug<3 until you can. Extensions such as Flask-Login 0.6.2 fail the same way.

  • Applies to Flask 3.1.3, Werkzeug 3.1.9, Flask-Login 0.6.3, Python 3.9 or later
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Print the installed versions, then read the file path in the last frame of the traceback. Flask below 2.2.4 with Werkzeug 3.x points to cause 1. A path inside another package's folder points to cause 2, and a path in your own project to cause 3.

Run this first
python -m pip show flask werkzeug flask-login | grep -E '^(Name|Version)'

Then find the line you see in this list and open its fix:

  1. 1An old Flask release was installed next to Werkzeug 3pip show prints Flask 2.2.3 or lower with Werkzeug 3.x, and the traceback ends in flask/app.py or flask/helpers.py
  2. 2A Flask extension still calls the removed helpersThe last traceback frame sits in site-packages/flask_login or another extension, and the missing name may be url_decode or url_encode instead of url_quote
  3. 3Your own code imports url_quote or its siblingsThe traceback points to a file in your repository with a from werkzeug.urls import line

What does the error look like?

  • ImportError: cannot import name 'url_quote' from 'werkzeug.urls'
  • from werkzeug.urls import url_quote
  • ImportError: cannot import name 'url_decode' from 'werkzeug.urls'

Cause 1 of 3

An old Flask release was installed next to Werkzeug 3

You seepip show prints Flask 2.2.3 or lower with Werkzeug 3.x, and the traceback ends in flask/app.py or flask/helpers.py

Check

Flask 2.2.0 to 2.2.3 import url_quote from werkzeug.urls in flask/app.py, and Flask 2.0 and 2.1 do it in flask/helpers.py. Their install rules ask only for a minimum Werkzeug, so pip picks the newest one. Werkzeug 2.3 deprecated the werkzeug.urls helpers and 3.0 deleted them, so the import fails before your app runs.

Check
grep -rn "werkzeug.urls" $(python -c 'import flask, os; print(os.path.dirname(flask.__file__))')

Fix

Move to a current Flask; 2.2.4 was the first release updated for Werkzeug 2.3, and Flask 3.1.3 needs Werkzeug 3.1 or later. If the app cannot move yet, pin Werkzeug to the 2.x line in the same requirements file as a short-term patch.

Fix
# preferred
python -m pip install --upgrade "flask==3.1.3" "werkzeug==3.1.9"
# stop-gap for an app stuck on Flask 2.2.3 or older
python -m pip install "werkzeug<3"

Verify

The app module imports and the server starts without the ImportError.

Verify
python -c "import flask, importlib.metadata as m; print(m.version('flask'), m.version('werkzeug'))"

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 3

A Flask extension still calls the removed helpers

You seeThe last traceback frame sits in site-packages/flask_login or another extension, and the missing name may be url_decode or url_encode instead of url_quote

Check

Extensions built before Werkzeug 3 import the same helpers. Flask-Login 0.6.2 imports url_decode and url_encode from werkzeug.urls, so it breaks even on a new Flask. Its 0.6.3 release added compatibility with Flask 3 and Werkzeug 3.

Check
python -m pip show flask-login | grep Version

Fix

Upgrade the extension to a release that supports Werkzeug 3. For Flask-Login that is 0.6.3. If an extension has no such release, replace it, or keep Werkzeug below 3 until it is gone.

Fix
python -m pip install --upgrade "flask-login==0.6.3"

Verify

Importing the extension works on Werkzeug 3.

Verify
python -c "import flask_login; print('ok')"

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 3

Your own code imports url_quote or its siblings

You seeThe traceback points to a file in your repository with a from werkzeug.urls import line

Check

Werkzeug 2.3 marked the werkzeug.urls module as deprecated except uri_to_iri and iri_to_uri, and told users to switch to the standard urllib.parse library. Code that still imports url_quote, url_parse or url_encode stops working on 3.0.

Check
grep -rn "from werkzeug.urls import" --include='*.py' .

Fix

Use the standard library: quote for url_quote, urlsplit for url_parse, urlencode for url_encode and parse_qs for url_decode. The return types differ: urlsplit gives a SplitResult tuple and parse_qs a dict of lists, so check what your code reads from them.

Fix
# before
from werkzeug.urls import url_quote, url_encode
# after
from urllib.parse import quote, urlencode
safe_path = quote(file_name)
query = urlencode({"page": 2, "q": "red shoes"})

Verify

grep finds no werkzeug.urls imports left except uri_to_iri or iri_to_uri, and the test suite passes.

Verify
grep -rn "werkzeug.urls" --include='*.py' .

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Write Flask and Werkzeug side by side with exact versions in requirements.txt, and change them in the same commit.
  • Run the tests with python -W error::DeprecationWarning -m pytest, so a helper that is deprecated today fails in CI before the next major release removes it.
  • Build the Docker image in CI on every dependency change and import the app module in it, so a new resolver result breaks the build, not production.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: stegnor.com, Menulo, ShopGo.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.