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

We Are Going To Discuss About ImportError: cannot import name ‘Markup’ from ‘jinja2’. So lets Start this Python Article.

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

  1. How to solve ImportError: cannot import name 'Markup' from 'jinja2'

    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('')
     

  2. ImportError: cannot import name 'Markup' from 'jinja2'

    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 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('')
Advertisements

Original Author Ged Flod Of This Content

Solution 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
Advertisements

Original Author Ged Flod Of This Content

Solution 3

I guess this works:

from markupsafe import Markup

Saw it on Github

Advertisements

Original Author Ali İltizar Of This Content

Solution 4

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
Advertisements

Original Author Ged Flod Of This Content

Conclusion

Advertisements

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment