You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Werkzeug导入url_quote报错,现有解决方案无效求助

解决Azure部署Flask应用时Werkzeug url_quote导入错误(Python 3.12)

我知道这个问题已有相关提问,在此致歉,但尝试各类解决方案均未奏效。我是编程新手,首次在Azure部署Web应用,始终遇到ImportError,无法正确导入Werkzeug的url_quote。

我尝试了适配不同Werkzeug版本的导入方式,并做了降级实现:

try:
    from werkzeug.urls import url_quote  # Werkzeug 0.15.0 and newer
except ImportError:
    try:
        from werkzeug import url_quote  # Werkzeug 0.14.x
    except ImportError:
        # Define a fallback implementation of url_quote using quote_plus from urllib.parse
        def url_quote(s):
            if isinstance(s, str):
                s = s.encode("utf-8")
            return quote_plus(s)

我的requirements.txt配置如下:

requests
openai
Flask==2.2.2
Werkzeug==2.3.7

这是我试过的最新版本组合,使用Python 3.12。

可行的解决步骤:

  • 补全fallback代码的导入:当前降级逻辑中未导入quote_plus,会触发NameError,需在最后一个except块内添加from urllib.parse import quote_plus,修正后的代码:
    try:
        from werkzeug.urls import url_quote  # Werkzeug 0.15.0 and newer
    except ImportError:
        try:
            from werkzeug import url_quote  # Werkzeug 0.14.x
        except ImportError:
            from urllib.parse import quote_plus
            def url_quote(s):
                if isinstance(s, str):
                    s = s.encode("utf-8")
                return quote_plus(s)
    
  • 强制Azure重新安装依赖:部署时添加启动命令,确保依赖完整安装:
    pip install --upgrade pip && pip install -r requirements.txt
    
  • 调整Werkzeug版本适配Python 3.12:Python 3.12对部分旧版库存在兼容问题,尝试将Werkzeug降级到2.2.x版本,修改requirements.txt:
    requests
    openai
    Flask==2.2.2
    Werkzeug==2.2.3
    
  • 确认Azure运行时版本:检查Azure Web Apps的配置,确保Python运行时设置为3.12,避免版本不匹配导致的依赖错误。
  • 本地环境复现排查:在本地Python 3.12虚拟环境中安装指定依赖,运行代码验证。如果本地正常,说明是Azure部署环境缓存问题,可尝试重置部署槽或清理依赖缓存。

内容的提问来源于stack exchange,提问作者James Vanderhaak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 15:06:17