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

运行Dash应用遇ImportError:无法从jinja2导入escape求助

ImportError: cannot import name 'escape' from 'jinja2' when running Dash app

Let’s break down what’s causing this error and how to fix it step by step:

Your Error Context

You’re hitting an import conflict when trying to run your Dash app, with the traceback pointing to a missing escape function in Jinja2. Here’s the full error you shared:

Traceback (most recent call last):
  File "/Users/lukeneuendorf/nfl_dashboard/index.py", line 1, in <module>
    import dash_core_components as dcc
  File "/opt/anaconda3/lib/python3.9/site-packages/dash_core_components/__init__.py", line 1, in <module>
    from dash.dcc import *  # noqa: F401, F403, E402
  File "/opt/anaconda3/lib/python3.9/site-packages/dash/__init__.py", line 5, in <module>
    from .dependencies import (  # noqa: F401,E402
  File "/opt/anaconda3/lib/python3.9/site-packages/dash/dependencies.py", line 4, in <module>
    from ._validate import validate_callback
  File "/opt/anaconda3/lib/python3.9/site-packages/dash/_validate.py", line 5, in <module>
    import flask
  File "/opt/anaconda3/lib/python3.9/site-packages/flask/__init__.py", line 14, in <module>
    from jinja2 import escape
ImportError: cannot import name 'escape' from 'jinja2' (/opt/anaconda3/lib/python3.9/site-packages/jinja2/__init__.py)

Your requirements.txt currently lists:

dash==2.7.1
dash-core-components==2.0.0
Flask==2.0.1
Jinja2==3.0.3
pandas==1.5.1
plotly==5.3.1

Root Causes

  1. Redundant dash-core-components package: Starting with Dash 2.0, dash_core_components is built directly into the main Dash library as dash.dcc. Installing the separate dash-core-components package (especially an older version like 2.0.0) creates unnecessary conflicts with your newer Dash 2.7.1 installation.
  2. Flask-Jinja2 version mismatch: Flask 2.0.1 expects to import escape from jinja2, but Jinja2 3.0+ moved this function to the markupsafe library. This mismatch is what’s throwing the import error.

Step-by-Step Fixes

  1. Remove the outdated dash-core-components dependency

    • Uninstall it first:
      pip uninstall -y dash-core-components
      
    • Update your index.py to use the built-in Dash import instead:
      # Replace this line
      # import dash_core_components as dcc
      # With this:
      from dash import dcc
      
    • Delete the dash-core-components==2.0.0 line from your requirements.txt.
  2. Fix the Flask-Jinja2 compatibility issue
    You have two valid options here:

    • Option 1: Upgrade Flask to support Jinja2 3.x
      Flask versions 2.1.0 and later fixed this import by pulling escape from markupsafe instead of jinja2. Install the updated Flask:

      pip install Flask>=2.1.0
      

      Update your requirements.txt to use Flask>=2.1.0 instead of the fixed 2.0.1 version.

    • Option 2: Downgrade Jinja2 to work with Flask 2.0.1
      If you need to keep Flask 2.0.1 for some reason, downgrade Jinja2 to a 2.x version that still has the escape function in the right place:

      pip install Jinja2<3.0
      

      Update your requirements.txt to Jinja2<3.0.

  3. Re-sync dependencies (optional but recommended)
    To make sure all packages are properly aligned, reinstall from your updated requirements.txt:

    pip install -r requirements.txt --force-reinstall
    

Example Updated requirements.txt

If you go with Option 1 (upgrading Flask), your final requirements file would look like this:

dash==2.7.1
Flask>=2.1.0
Jinja2==3.0.3
pandas==1.5.1
plotly==5.3.1

内容的提问来源于stack exchange,提问作者Luke Neuendorf

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 10:35:41