ImportError: cannot import name ‘Markup’ from ‘jinja2’

When I recently deployed my project that includes Flask==1.0.2 and Jinja2>=2.10.1, I got the following error. It was running fine when I deployed it the previous day. I tried updating Jinja2 but that didn’t fix the issue.

 

requirements.txt:

attrs>=19.1.0
boto3==1.17.23
click==7.0
itsdangerous==2.0.1
flasgger==0.9.5
Flask==1.0.2
Flask-RESTful>=0.3.6
flask-cors==3.0.8
gunicorn==20.1.0
Jinja2>=2.10.1
jsonschema>=3.0.1,<4.0
marshmallow>=3.0,<=3.6
marshmallow3-annotations>=1.0.0
pytz==2021.1
requests>=2.25.0
requests-aws4auth==1.1.0
statsd==3.2.1
typing==3.6.4
werkzeug>=2.0.0
wheel==0.36.2
itsdangerous==2.0.1 Answer 1:

Version 3.0.1

Regarding the documentation

Fixed calling deprecated jinja2.Markup without an argument. Use markupsafe.Markup instead. #1438

So to import Markup use the following code :

>>> from jinja2.utils import markupsafe 
>>> markupsafe.Markup()
Markup('')

Answer 2:

As the import error comes from flask File "/usr/local/lib/python3.7/site-packages/flask/__init__.py

I tried to create a new Flask application using Flask==1.0.2 and found that the error comes from this version of Flask when it is used with Jinja2>=2.10.1.

But when you remove Flask==1.0.2 and install Flask==2.0.3, everything works fine.

pip uninstall  Flask Jinja2
pip install Flask Jinja2

Dependencies

pip freeze
click==8.0.4
Flask==2.0.3
itsdangerous==2.1.2
Jinja2==3.1.1
MarkupSafe==2.1.1
Werkzeug==2.0.3