'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.
File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
self.wsgi = self.app.wsgi()
File "/usr/local/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/usr/local/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
return self.load_wsgiapp()
File "/usr/local/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
return util.import_app(self.app_uri)
File "/usr/local/lib/python3.7/site-packages/gunicorn/util.py", line 359, in import_app
mod = importlib.import_module(module)
File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/app/search_service/__init__.py", line 12, in <module>
from flasgger import Swagger
File "/usr/local/lib/python3.7/site-packages/flasgger/__init__.py", line 10, in <module>
from .base import Swagger, Flasgger, NO_SANITIZER, BR_SANITIZER, MK_SANITIZER, LazyJSONEncoder # noqa
File "/usr/local/lib/python3.7/site-packages/flasgger/base.py", line 19, in <module>
from flask import Blueprint
File "/usr/local/lib/python3.7/site-packages/flask/__init__.py", line 19, in <module>
from jinja2 import Markup, escape
ImportError: cannot import name 'Markup' from 'jinja2' (/usr/local/lib/python3.7/site-packages/jinja2/__init__.py)
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
Solution 1:[1]
Version 3.0.1
Regarding to 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('')
Solution 2:[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 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
Solution 3:[3]
Solution 4:[4]
The actual stable version of jinja is 3.x try to update jinja:
- Uninstall Jinja2==2.10.1
pip uninstall jinja2
- Install the latest version of jinja2
pip install --upgrade jinja2
Solution 5:[5]
This might be a result of using a depreciated version of jinja2. In that case, uninstall jinja2 using pip uninstall jinja2
Then, install a stable version. You can install version 3 using pip install jinja2==3.0
Solution 6:[6]
I solved a similar issue by running pip install --upgrade <package-name>
on all individual packages of my flask app. This means that I did;
pip install --upgrade babel
pip install --upgrade python-dateutil
pip install --upgrade flask-moment
pip install --upgrade flask-wtf
pip install --upgrade flask_sqlalchemy
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | |
Solution 3 | Ali ?ltizar |
Solution 4 | |
Solution 5 | Donald Duck |
Solution 6 | Eunit |