ImportError: cannot import name 'escape' from 'jinja2'
Asked Answered
H

9

129

I am getting the error

ImportError: cannot import name 'escape' from 'jinja2'

When trying to run code using the following requirements.txt:

chart_studio==1.1.0
dash==2.1.0
dash_bootstrap_components==1.0.3
dash_core_components==2.0.0
dash_html_components==2.0.0
dash_renderer==1.9.1
dash_table==5.0.0
Flask==1.1.2
matplotlib==3.4.3
numpy==1.20.3
pandas==1.3.4
plotly==5.5.0
PyYAML==6.0
scikit_learn==1.0.2
scipy==1.7.1
seaborn==0.11.2
statsmodels==0.12.2
urllib3==1.26.7

Tried

pip install jinja2

But the requirement is already satisfied.
Running this code on a windows system.

Hallelujah answered 2/4, 2022 at 13:56 Comment(0)
L
181

Jinja is a dependency of Flask and Flask V1.X.X uses the escape module from Jinja, however recently support for the escape module was dropped in newer versions of Jinja.

To fix this issue, simply update to the newer version of Flask V2.X.X in your requirements.txt where Flask no longer uses the escape module from Jinja.

Flask>=2.2.2

Also, do note that Flask V1.X.X is no longer supported by the team. If you want to continue to use this older version, this Github issue may help.

Lightly answered 4/4, 2022 at 9:45 Comment(1)
a useful answer for updating Flask: https://mcmap.net/q/175500/-how-to-update-flask-from-version-1-1-2-to-version-2-0-on-anacondaParochial
P
60

This happens because Jinja has removed those functions in a recent version — 3.1.0 — released on March 24th, 2022.

Markup and escape should be imported from MarkupSafe.

You have two options form here:

  1. either this error comes from one of your dependency.
    The first thing you should consider is to upgrade the said dependence(s).
    If this is not possible, what you can do, from here is to downgrade your Jinja version to a version that would still include escape, for example, adding it explicitly in your requirements.txt:

    jinja2<3.1.0
    
  2. or, your error is from code you wrote, so you can fix it by importing it from MarkupSafe, as suggested in the Jinja release notes.

    So, you should use

    from markupsafe import escape
    

    instead of

    from jinja2 import escape
    
Putnam answered 2/4, 2022 at 20:40 Comment(1)
Worked perfect with jinja2<3.1.0. Thanks for sharing! I also needed to add werkzeug==2.0.3.Venettavenezia
Y
16

Simply update your Flask version, it works for me

Flask==2.1.0
Yes answered 12/7, 2022 at 13:9 Comment(3)
how to update flask version in ubuntu?Sweepings
check this guide linuxize.com/post/how-to-install-flask-on-ubuntu-20-04Yes
pip install Flask==2.1.0Lusaka
I
9

So what happened to me is that I cloned a repo on github and installed flask with my virtualenv activated using the command pip install -r requirements.txt but when I tried to run the server thats when it have me the error:

ImportError: cannot import name 'escape' from 'jinja2'

So what I did is ran the command pip uninstall flask and reinstalled it by running pip install flask and then ran my server again, then it worked.

SIMPLE SOLUTION

pip uninstall flask

then

pip install flask 
Insulin answered 17/6, 2022 at 2:31 Comment(1)
Fixed installing AWS SAM in a docker image today! SAVED ME!Karoline
B
5

As per change logs for version >= 3.1.x, need to use escape from markupsafe not from jinja2

so to use escape do

from markupsafe import escape
Bewley answered 2/5, 2023 at 19:3 Comment(0)
L
1

ImportError: cannot import name 'escape' from 'jinja2'

This happened to me using Voila with jupyter notebook and solved using method below:

  1. going to this directory C:\Users\admin\anaconda3\Lib\site-packages\nbconvert\filters\ansi.py
  2. adding this line to the first of file from markupsafe import escape
  3. Also change this line of code text = jinja2.utils.escape(text) to text = escape(text)
Laborer answered 8/5, 2022 at 8:13 Comment(0)
S
0

Issue resolved by just downgrading jinja2 to low stable version,

pip install Jinja2==3.0.3

reference

Software answered 18/3, 2023 at 10:28 Comment(0)
J
0

This worked for me!

!pip install --upgrade babel
!pip install --upgrade python-dateutil
!pip install --upgrade flask-moment
!pip install --upgrade flask-wtf
!pip install --upgrade flask_sqlalchemy
Jerusalem answered 11/4, 2023 at 23:52 Comment(0)
C
0
from html import escape 

This is what worked for me.

Clean answered 29/1 at 12:58 Comment(3)
SilkLife, please don't add thanks as answers. They don't actually provide an answer to the question, and can be perceived as noise by its future visitors. Once you earn enough reputation, you will gain privileges to upvote answers you like. This way future visitors of the question will see a higher vote count on that answer, and the answerer will also be rewarded with reputation points. See Why is voting important.Feathery
Ok! Can i answer in the comments?Clean
@mozway: This appears to be a new answer, just poorly worded.Directly

© 2022 - 2024 — McMap. All rights reserved.