52

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
Gedeon Mutshipayi
  • 2,871
  • 3
  • 21
  • 42
young_minds1
  • 1,181
  • 3
  • 10
  • 25

12 Answers12

38

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
Gedeon Mutshipayi
  • 2,871
  • 3
  • 21
  • 42
21

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 markupsafe import Markup 
>>> Markup()
Markup('')
davidism
  • 121,510
  • 29
  • 395
  • 339
Gedeon Mutshipayi
  • 2,871
  • 3
  • 21
  • 42
8

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;

  1. pip install --upgrade babel
  2. pip install --upgrade python-dateutil
  3. pip install --upgrade flask-moment
  4. pip install --upgrade flask-wtf
  5. pip install --upgrade flask_sqlalchemy

requirements.txt

Eunit
  • 107
  • 1
  • 3
  • 1
    This answer worked for me. I have upgraded the jinja2 and markupsafe by running pip install --upgrade jinja2 and pip install --upgrade markupsafe then pip freeze > requirement.txt – Fatimah Mohmmed Jun 21 '22 at 10:25
5

I guess this works:

from markupsafe import Markup

Saw it on Github

  • 3
    the answer you are arguing can be supplemented with more information please add more information by editing and also read [how to write a good answer](https://stackoverflow.com/help/how-to-answer) in the [help center](https://stackoverflow.com/help) – user11717481 Apr 24 '22 at 15:36
  • this seems to work but difficult to comprehend – semmyk-research Mar 25 '23 at 10:31
4

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

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
  • This solved the cannot import Markup but now there is cannot import name json from itsdangerous – KansaiRobot Sep 01 '22 at 04:41
  • @KansaiRobot, do you find a better solution? I am having the same error. I have deployed successfully but in browsing, it shows an application error (cannot import name 'Markup' from 'jinja2') – Fatimah Mohmmed Sep 07 '22 at 05:39
  • actually, I just gave up trying to use a requirements file. Instead I installed each necessary function manually one by one – KansaiRobot Sep 07 '22 at 11:53
2

The actual stable version of jinja is 3.x try to update jinja:

  1. Uninstall Jinja2==2.10.1
pip uninstall jinja2
  1. Install the latest version of jinja2
pip install --upgrade jinja2
Gedeon Mutshipayi
  • 2,871
  • 3
  • 21
  • 42
1

run pip uninstall Werkzeug jinja2 then pip install Werkzeug==2.0.0
and pip install jinja2==3.0

Tony
  • 19
  • 1
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the [help center](https://stackoverflow.com/help/how-to-answer). – AlexK Jun 05 '22 at 05:01
  • Re: unclear. [For Noting] ... `pip install Werkzeug==2.0.0 jinja2==3.0` gives `ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts. flask 2.2.3 requires Werkzeug>=2.2.2, but you have werkzeug 2.0.0 which is incompatible.` – semmyk-research Mar 25 '23 at 09:59
0

Hey what you need to do is update all the requirements files by running

pip install --upgrade <requirements.txt>

or

pip install --upgrade <Do it manually>
vimuth
  • 5,064
  • 33
  • 79
  • 116
ahruf
  • 1
0

I faced this issue when using a python virtual environment, I had to remove flask and other libraries that were in the base python installation. I think some library I installed there was messing somehow with it.

0

TL;DR from markupsafe import Markup


I tried updating Jinja2 but that didn't fix the issue.

For some, simply updating (or uninstall and install) jinja2 and Flask works.

However, the root problem is a 'pending' PR (pull request): Fixed Markup import; it is now imported from markupsafe #32

To resolve this, do a git or simply edit the flask_jsglue.pyfile in the... lib\site-packages\flask_jsglue.py
edit from jinja2 import Markup to from markupsafe import Markup

#from jinja2 import Markup
from markupsafe import Markup

@Ged-Flod provided an excellent reference to the Jinja2 documentation: deprecation of jinja2.Markup.

semmyk-research
  • 333
  • 1
  • 9
0

I had the issue when trying to deploy with Zappa. The solution I found was to run.

pip install --platform=manylinux2014_x86_64 --only-binary=:all: --target venv/lib/python3.10/site-packages flask markupsafe jinja2==3.0.3 --force-reinstall --upgrade

0
pip install -r requirements.txt  

file requirements.txt such as:

Flask==1.1.2  
Flask-Cors==3.0.8  
Flask-RESTful==0.3.8  
Jinja2==2.11.2 
MarkupSafe==1.1.1  
itsdangerous==1.1.0  
Werkzeug==1.0.1

-> I think Flask version > 2.0 has this error!

moken
  • 3,227
  • 8
  • 13
  • 23
BKPHAN
  • 1