Why did Flask start failing with "ImportError: cannot import name 'url_quote' from 'werkzeug.urls'"?
Asked Answered
E

6

161

Environment:

Python 3.10.11
Flask==2.2.2

I run my Flask backend code in docker container, with BASE Image: FROM pytorch/pytorch:2.0.1-cuda11.7-cudnn8-runtime

But when I run the pytest with version pytest 7.4.2,

pip install pytest
pytest

it raised an Error, with logs:

==================================== ERRORS ====================================
_____________ ERROR collecting tests/test_fiftyone_utils_utils.py ______________
ImportError while importing test module '/builds/kw/data-auto-analysis-toolkit-backend/tests/test_fiftyone_utils_utils.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/opt/conda/lib/python3.10/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_fiftyone_utils_utils.py:2: in <module>
    import daat  # noqa: F401
/opt/conda/lib/python3.10/site-packages/daat-1.0.0-py3.10.egg/daat/__init__.py:1: in <module>
    from daat.app import app
/opt/conda/lib/python3.10/site-packages/daat-1.0.0-py3.10.egg/daat/app/__init__.py:6: in <module>
    from flask import Flask, jsonify, request
/opt/conda/lib/python3.10/site-packages/flask/__init__.py:5: in <module>
    from .app import Flask as Flask
/opt/conda/lib/python3.10/site-packages/flask/app.py:30: in <module>
    from werkzeug.urls import url_quote
E   ImportError: cannot import name 'url_quote' from 'werkzeug.urls' (/opt/conda/lib/python3.10/site-packages/werkzeug/urls.py)

My codes works well when I directly run it with python run.py

run.py shown below

from daat import app

app.run(host='0.0.0.0')

I guess it should be the pytest versions issue, because it used to work well without changing any related code, and I use pip install pytest without defined a specific version.

And my backend runs well without pytest.

Excrement answered 2/10, 2023 at 3:2 Comment(2)
Depreciated. Use urllib.parse.urlsplit instead.Solvent
I edited the title because it should be immediately clear that this is about a Flask installation problem, and not for people who are trying to use the Werkzeug API directly.Nert
E
260

I had the same problem. It is because Werkzeug 3.0.0 was released and Flask doesn't specify the dependency correctly (requirements says Werkzeug>=2.2.0). This is why, Werkzeug 3.0.0 is still installed and Flask 2.2.2 isn't made for Werkzeug 3.0.0.

Solution: Just set a fix version for Werkzeug such as Werkzeug==2.2.2 in your requirements.txt and it should work.

Expose answered 2/10, 2023 at 8:17 Comment(11)
Alternatively, you could use Werkzeug==2.3.x as this was the last version used according to the version specified by flask.Expose
This is the error I got: The conflict is caused by: The user requested Werkzeug==2.2; flask 2.2.2 depends on Werkzeug>=2.2.2Poinciana
When using flask==2.1.3, it seems forcing Werkzeug==2.3.7 solves the issue for us. According to our logs, that's the version of Werkzeug that still got resolved by pip as of end of last week.Sharpeared
Werkzeug>=2.2,<3.0 should also workHowdah
Can you do print(flask.__version__)? The pinning of werkzeug seems to only work by downgrading flask from 3.0. The discrepancy seems to work both ways - if you upgrade to flask 3.0 things go boom tooUtta
Can also confirm that using flask==2.2.2 and adding Werkzeug==2.3.7 fixed this issue for usSolo
In my case confirmed and user a previos repo version with pip show Werkzeug and used that version in my requirements.txt file in a line prior to Flask like Werkzeug==2.3.6Godiva
There is a recent security issue with Werkzeug. For the security issue you may need werkzeug>=3.0.1 see security.snyk.io/package/pip/werkzeug . It is unclear to me how to resolve the Flask-Werkzeug problem then.Catalano
This worked for me, I had to install Werkzeug==2.2.2 using the command pip install Werkzeug==2.2.2Diderot
Thank you for the great simply answer! I note also that applies to the python version on GCP i.e. need to specify - especially a lower version than the most recent available on marketEllaelladine
In early 2024, Flask 3.0.0 and Werkzeug 3.0.1 don't have this issue. Maybe this works for you, @FinnÅrupNielsenGilli
E
20

The root cause of this is that Werkzeug 3.0.0 removed previously deprecated code: https://werkzeug.palletsprojects.com/en/3.0.x/changes/#version-3-0-0

Please update your Flask version, Flask 2.2.2 is unsupported: https://github.com/pallets/flask/releases

Anyway, you need to pin Werkzeug yourself then if you insist on using a deprecated version of Flask, or if your code is using url_quote directly then you can switch to the built-in urllib:

from urllib.parse import quote as url_quote
Erratic answered 3/10, 2023 at 11:58 Comment(2)
This solution worked for us. The aforementioned one (pinning Werkzeug version) functions as well, but this one is IMHO the correct way.Urbano
Works for url_parse #from werkzeug.urls import url_parse from urllib.parse import urlparse as url_parse doc.python.org.Scar
A
10

I started getting this error in an update I deployed today, even though I wasn't trying to import "url_quote". Flask == 2.0.1 . Setting Werkzeug==2.2.2 also worked for me.

Absa answered 2/10, 2023 at 19:15 Comment(0)
G
7

Modify your requirements.txt to include:

Werkzeug==2.2.x or Werkzeug==2.3.x. Or use Werkzeug==2.2.2 to be safe.

Gahl answered 20/10, 2023 at 2:56 Comment(1)
Seems pretty much the same as many answers already posted.Execratory
M
5
connexion[swagger-ui]<3
flask>=2.0  
Werkzeug>=2.0
gunicorn>=20.0

This combination worked for me and resolved into this:

Successfully installed Werkzeug-2.2.3 connexion-2.14.2 flask-2.2.5

Python 3.11/3.12

Magdau answered 4/11, 2023 at 17:1 Comment(0)
P
1

If you're encountering an issue with the "url_quote" function in your Flask application, it's likely due to an incorrect import or a version conflict between Flask and Werkzeug.

To resolve this issue, follow the steps below:

  • Update Your Flask Version

This step ensures that you have the latest Flask version :

pip install --upgrade Flask

  • Update pytest

In some cases, the problem could be related to a pytest version conflict. You can try upgrading pytest to a version that is compatible with your environment using the following command :

pip install --upgrade pytest

  • Downgrade the Werkzeug Version

If updating Flask and resolving package conflicts doesn't solve the problemconsider using Werkzeug==2.3.x, be aware of dependency constraints, and force Werkzeug==2.3.7 with Flask==2.1.3 if needed. Specifying a Werkzeug version range like Werkzeug>=2.2,<3.0 is an adaptable option. Test with Flask==2.2.2 and Werkzeug==2.3.7 and verify Flask version compatibility. You can also specify the Werkzeug version in your requirements.txt file, e.g., Werkzeug==2.3.6. These steps should help manage version conflicts in your Flask application.

Padgett answered 17/10, 2023 at 9:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.