Introducing Livops 2.0! Promo codeCEOgives you 8,000 so‘m on your balance.What’s new

ImportError: cannot import name 'url_quote' from 'werkzeug.urls': how to fix it

ImportError: cannot import name 'url_quote' from 'werkzeug.urls' means a Flask from 2.0 to 2.2.3 was installed next to Werkzeug 3, which removed url_quote and almost all of werkzeug.urls. Flask 2.x asks only for a minimum Werkzeug, so a fresh install on the server picks the newest one while your laptop keeps the old one. Pin the Werkzeug that works (Werkzeug<3), or upgrade to Flask 3 together with your extensions.

By Livops team, the people who build and run LivopsUpdated

The app ran yesterday, and after a fresh deploy it stops before serving a single request. On Flask 2.2.3 the last lines of the traceback point inside Flask itself[5]:

  File ".../site-packages/flask/app.py", line 30, in <module>
    from werkzeug.urls import url_quote
ImportError: cannot import name 'url_quote' from 'werkzeug.urls' (.../site-packages/werkzeug/urls.py)

On Flask 2.0 or 2.1 the same import sits in flask/helpers.py[6], so the file name differs and the error is the same. This page explains why Werkzeug 3 breaks these Flask versions, why it happens on the server and not on your laptop, and the two fixes: pin Werkzeug, or upgrade Flask and its extensions.

What does the error mean?

Werkzeug is the library under Flask: the request, the response, the routing. Werkzeug 2.3.0 deprecated the whole werkzeug.urls module, except uri_to_iri and iri_to_uri, and told users to use Python’s urllib.parse instead[1]. Werkzeug 3.0.0, released on 30 September 2023, removed the code that was deprecated[1]. In 3.0.0, werkzeug/urls.py defines only those two public functions, uri_to_iri and iri_to_uri[2]; url_quote is gone, and so is every other url_* helper.

Flask from 2.0.0 to 2.2.3 imports url_quote from werkzeug.urls when it starts[5][6]. With Werkzeug 3 installed, that import fails, and Python raises ImportError before your own code runs. Flask 2.2.4 changed the import to from urllib.parse import quote as _url_quote[8]; its changelog says it was updated for compatibility with Werkzeug 2.3[4].

The same removal, other names

The same Werkzeug release removed the rest of werkzeug.urls, so the same error can name another function[2][3]:

The error names Where it usually comes from Use instead
url_quote Flask 2.0 to 2.2.3 at start Flask 2.2.4+, or urllib.parse.quote
url_parse flask/testing.py in Flask 2.2.3 and older, when tests run; your own code urllib.parse.urlsplit
url_decode, url_encode Flask-Login 0.6.2; Flask-WTF’s reCAPTCHA before 1.1.2 urllib.parse.parse_qs, urllib.parse.urlencode
url_unquote, url_join, url_unparse your own code or an old extension urllib.parse.unquote, urljoin, urlunsplit

Flask 2.2.3’s test client imports url_parse[7], so the error can appear only in CI while the app itself starts. Flask-Login 0.6.2 imports url_decode and url_encode[12], and 0.6.3 is the release marked compatible with Flask 3 and Werkzeug 3[11]. Flask-WTF fixed its werkzeug.urls.url_encode import in 1.1.2[13]. The replacements in the last column are the ones Werkzeug 2.3’s own deprecation warnings name[3], all from Python’s standard library[19].

Why it breaks on deploy and not locally

Flask 2.x declares a minimum Werkzeug and no maximum: Flask 2.0 and 2.1 ask for Werkzeug >= 2.0[9], and Flask 2.2 for Werkzeug >= 2.2.2[10]. Werkzeug 3.0.0 satisfies both. Two installs of the same requirements.txt then end differently:

  • Your laptop installed Werkzeug when the project started, before 3.0 existed or before you last changed anything. pip install prefers to leave the installed version as it is unless --upgrade is given[16], so running it again keeps the old Werkzeug.
  • A fresh environment has nothing installed. pip starts from the most recent version of a package[17], so it takes the newest Werkzeug that Flask’s minimum allows; on PyPI that is now the 3.1 series[14].

That is what pip’s own guide on repeatable installs warns about: pinning protects you from bugs or incompatibilities in newly released versions[18]. A requirements.txt that lists flask==2.2.3 and leaves Werkzeug out pins Flask and nothing under it.

Fix 1: pin Werkzeug below 3

The fastest fix, with no code changes. In requirements.txt:

flask==2.2.3
werkzeug<3

Better still, pin the exact Werkzeug your laptop runs, because it is the one you have tested. Find it in the environment where the app works:

.venv/bin/python -m pip show werkzeug

and write that version in, for example werkzeug==2.2.3. The last 2.x release of Werkzeug is 2.3.8[14]. With a Flask older than 2.2.4, stay on the Werkzeug you tested rather than jumping to 2.3: Flask 2.2.4 was released to be compatible with Werkzeug 2.3[4].

To pin everything at once, write the working environment out with pip freeze, which produces a requirements file of pinned versions[18]:

.venv/bin/python -m pip freeze > requirements.txt

Fix 2: upgrade Flask and its extensions

The lasting fix is a Flask that does not import from werkzeug.urls. Flask 3.0.0 requires Werkzeug 3.0.0 or newer[4], and Flask 3.1.0 raised that to Werkzeug 3.1[4], so the two are released to work together. The current Flask on PyPI is 3.1.3[15].

flask>=3.1
flask-login>=0.6.3

Plan for more than a version bump. Flask 2.3.0 and 3.0.0 each removed code that was deprecated before[4], so an app that ran with warnings on Flask 2.2 can stop on something else, and every extension must be a version that supports Werkzeug 3. Upgrade in a branch, run the app and its tests locally from a new virtual environment (below), and deploy when that starts clean.

If your own code imports from werkzeug.urls, change it to urllib.parse. The Flask login pattern that checks the next parameter becomes:

from urllib.parse import urlsplit

next_page = request.args.get("next")
if not next_page or urlsplit(next_page).netloc != "":
    next_page = url_for("index")

urlsplit returns the parts of the URL, and netloc is the network location[19]: empty for a path on your own site, set for a link to another one.

How to reproduce it locally

The error hides on your laptop because the old Werkzeug is still installed. Do what the server does, from nothing:

python3 -m venv /tmp/fresh-venv
/tmp/fresh-venv/bin/python -m pip install -r requirements.txt
/tmp/fresh-venv/bin/python -m pip show werkzeug
/tmp/fresh-venv/bin/python -c "import app"

If pip show prints a 3.x version and the import fails with the same ImportError, you are seeing what the server sees. After Fix 1 or Fix 2, delete /tmp/fresh-venv, run the same lines again, and the import should finish without an error.

On Livops

  • Dependencies are installed on the server. Livops installs your requirements.txt itself on each build, much like the fresh environment above, so an unpinned Werkzeug can resolve to a newer version on any deploy. Pin it in requirements.txt and the next deploy installs that version.

  • Do not upload your .venv. When you upload a folder, venv and .venv are dropped automatically; we install dependencies ourselves.

  • A failed start keeps the old version. This error stops the app as it starts, so the deploy fails as start_failed. The traceback is on the Logs page, and the assistant can read the log and explain what happened.

  • The start command. For a Flask app with Gunicorn in the requirements, Livops starts .venv/bin/gunicorn app:app --bind 0.0.0.0:3000; to set your own, put "start" in livops.json. Without Gunicorn in the requirements it starts Flask’s own runner, .venv/bin/python -m flask --app app run --host 0.0.0.0 --port 3000, which is the development server. More in Flask hosting.

  • Choose the Python. Livops runs Python 3.10 to 3.14. A .python-version file at the project root pins it:

    3.12

A Python web app costs 39 credits a month. When the error is No module named rather than cannot import name, the package is not installed at all: see ModuleNotFoundError: No module named.

Questions

Which Flask versions have this error?

Flask 2.0.0 to 2.2.3. In 2.0 and 2.1 the import is in flask/helpers.py, in 2.2.0 to 2.2.3 in flask/app.py. Flask 2.2.4 and later import quote from Python's own urllib.parse instead, so they do not stop on this line.

Why does it work on my laptop and fail on the server?

Your laptop still has the Werkzeug that was installed when the project started, and pip leaves an installed version alone unless you pass --upgrade. The server installs from scratch, and Flask 2.x asks only for a minimum Werkzeug, so pip takes the newest one, which is 3.x.

Should I pin Werkzeug or upgrade Flask?

Pin Werkzeug below 3 to get the site back up today: it is a one-line change and no code changes. Upgrade to Flask 3 when you have time to test, because Flask 2.3 and 3.0 removed code that was deprecated, and your extensions need versions that support Werkzeug 3 too.

Which Werkzeug version should I pin?

The one that works locally: run pip show werkzeug in the environment where the app runs and pin that exact version. Werkzeug<3 is the short form; the last 2.x release is 2.3.8.

I upgraded Flask and now get url_decode or url_encode instead. Why?

Another package still imports from werkzeug.urls. Flask-Login 0.6.2 imports url_decode and url_encode, and Flask-WTF fixed its url_encode import in 1.1.2. Upgrade the package the traceback names: Flask-Login 0.6.3 is compatible with Werkzeug 3.

My own code uses url_parse from werkzeug.urls. What replaces it?

urllib.parse.urlsplit, from Python's standard library. Werkzeug 2.3's own deprecation warnings name the replacement for each function: quote for url_quote, unquote for url_unquote, urlencode for url_encode, parse_qs for url_decode, urljoin for url_join.

Which Python versions does Livops run?

Python 3.10 to 3.14. Put the version in a .python-version file at the project root, for example 3.12.

Sources

  1. Werkzeug — Changes (werkzeug.palletsprojects.com). Checked .
  2. Werkzeug source — src/werkzeug/urls.py (3.0.0) (raw.githubusercontent.com). Checked .
  3. Werkzeug source — src/werkzeug/urls.py (2.3.8) (raw.githubusercontent.com). Checked .
  4. Flask — Changes (flask.palletsprojects.com). Checked .
  5. Flask source — src/flask/app.py (2.2.3) (raw.githubusercontent.com). Checked .
  6. Flask source — src/flask/helpers.py (2.1.3) (raw.githubusercontent.com). Checked .
  7. Flask source — src/flask/testing.py (2.2.3) (raw.githubusercontent.com). Checked .
  8. Flask source — src/flask/app.py (2.2.4) (raw.githubusercontent.com). Checked .
  9. Flask source — setup.py (2.1.3) (raw.githubusercontent.com). Checked .
  10. Flask source — setup.py (2.2.5) (raw.githubusercontent.com). Checked .
  11. Flask-Login — Changelog (raw.githubusercontent.com). Checked .
  12. Flask-Login source — src/flask_login/utils.py (0.6.2) (raw.githubusercontent.com). Checked .
  13. Flask-WTF — Changes (flask-wtf.readthedocs.io). Checked .
  14. PyPI — Werkzeug (pypi.org). Checked .
  15. PyPI — Flask (pypi.org). Checked .
  16. pip docs — pip install (pip.pypa.io). Checked .
  17. pip docs — Dependency Resolution (pip.pypa.io). Checked .
  18. pip docs — Repeatable Installs (pip.pypa.io). Checked .
  19. Python docs — urllib.parse (docs.python.org). Checked .

Put your project online today

Connect GitHub or drop a folder. No card is asked for.

Deploy your Flask app

Got a question? — @livops_uz