环境中Python 3.10.11,Flask==2.2.2,执行pip install pytest命令报错:ImportError: cannot import name 'url_quote' from 'werkzeug.urls',下面记录一下这个报错的解决方法。

报错信息:

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
import daat # noqa: F401
/opt/conda/lib/python3.10/site-packages/daat-1.0.0-py3.10.egg/daat/init.py:1: in
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
from flask import Flask, jsonify, request
/opt/conda/lib/python3.10/site-packages/flask/init.py:5: in
from .app import Flask as Flask
/opt/conda/lib/python3.10/site-packages/flask/app.py:30: in
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)

问题原因:

Werkzeug 3.0.0删除了以前不推荐的代码,Flask没有正确指定依赖项。安装Werkzeug 3.0.0,而Flask 2.2.2和Werkzeug 3.0.0不兼容。

解决方法:

pip3 install Werkzeug==2.2.2

或者

尝试更新flask版本

或者

可以直接切换到内置的urllib:

from urllib.parse import quote as url_quote

参考文档:

https://werkzeug.palletsprojects.com/en/3.0.x/changes/#version-3-0-0

https://github.com/pallets/flask/releases

推荐文档

相关文档

大家感兴趣的内容

随机列表