运行Dash应用遇ImportError:无法从jinja2导入escape求助
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
- Redundant
dash-core-componentspackage: Starting with Dash 2.0,dash_core_componentsis built directly into the main Dash library asdash.dcc. Installing the separatedash-core-componentspackage (especially an older version like 2.0.0) creates unnecessary conflicts with your newer Dash 2.7.1 installation. - Flask-Jinja2 version mismatch: Flask 2.0.1 expects to import
escapefromjinja2, but Jinja2 3.0+ moved this function to themarkupsafelibrary. This mismatch is what’s throwing the import error.
Step-by-Step Fixes
Remove the outdated
dash-core-componentsdependency- Uninstall it first:
pip uninstall -y dash-core-components - Update your
index.pyto 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.0line from yourrequirements.txt.
- Uninstall it first:
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 pullingescapefrommarkupsafeinstead ofjinja2. Install the updated Flask:pip install Flask>=2.1.0Update your
requirements.txtto useFlask>=2.1.0instead 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 theescapefunction in the right place:pip install Jinja2<3.0Update your
requirements.txttoJinja2<3.0.
Re-sync dependencies (optional but recommended)
To make sure all packages are properly aligned, reinstall from your updatedrequirements.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

