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

Question:

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.

Asked By: stevezkw

||

Answers:

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.

Answered By: Scrashdemix

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.

Answered By: Eye Scream

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 an outdated 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
Answered By: aude

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.

Answered By: XMehdi01

Modify your requirements.txt to include:

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

Answered By: Liam Swayne
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

Answered By: Henadzi Rabkin
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.