importerror: cannot import name ‘escape’ from ‘jinja2’ is a very simple error that users are facing after the release of jinja2 version 3.1.0. If you have upgraded jinja2 to 3.1.0 or above from the previous version and if you are using the escape module in your python code then you will face this error message.
Don’t worry. Go through this article thoroughly to understand the error message, its root cause, and the possible solution. Stay tuned and keep reading.
Table of Contents
What is meant by importerror: cannot import name ‘escape’ from ‘jinja2’?
To understand the error message in a better way let’s break the error message into two parts as explained below.
Importerror: Importerror means there is an issue while importing the package into the project.
cannot import name ‘escape’ from ‘jinja2’: This error message clearly mentions that the project is not able to import the escape module from the jinja2 package.
Why you are getting importerror: cannot import name ‘escape’ from ‘jinja2’?
From the version 3.1.0 escape module is dropped from jinja2 and moved into the markupsafe package. If you are using a jinja2 version greater than 3.1.0 and in your code, if you are still using the import statement for the escape package as from jinja2 import escape then you will face the error message as an escape is no longer available in jinja2 package.
The following statement is mentioned in the official release of version 3.1.0
Markup and escape should be imported from MarkupSafe
Check more information on the official release bulletin.
How to solve importerror: cannot import name ‘escape’ from ‘jinja2’?
You can resolve this error message either by downgrading the jinja2 version to a lower supported version or by importing escape from a new module or by pointing to a lower version of jinja2. Let’s see all the approaches to resolve the error message in detail one by one.
1. Downgrade jinja2 to lower supported versions:
escape module is moved from the jinja2 package to markupsafe from version 3.1.0. If you use version lower that 3.1.0 then you will not get this error message because escape module is available in the jinja2 packages lower than 3.1.0.
Download and install the jinja2 version lower than 3.1.0 using the following command.
pip install jinja2==<version-of-jinja2-you-want-to-install>
You can install the 3.0.3 version of jinja2 using the following command.
pip install jinja2==3.0.3
2. Point to the lower version of the jinja2 package:
If it’s not possible to downgrade the version of jinja2 then you can explicitly point to the lower version of jinja2 in the requirement.txt file as shown below.
3. Import escape from a new module
Change import escape module from jinja2 to markupsafe.
To achieve this change import statement of the escape module in the code from
jinja2 import escape
markupsafe import escape
We at Technolads recommend to use third approach of solving the import error. Downgrading jinja2 to lower version solves the problem temporarily but you will miss some important new features that are available in latest version of jinja2.
In the future release of jinja2 some important new features may not support for older versions. Hence we recommend to use latest version of jinja2 and make small change in the import statement of escape module as explained in the third approach.
We hope this article was informative and you are able to get rid of importerror: cannot import name ‘escape’ from ‘jinja2’ error by using one of the above approaches. If you are still facing the issue then please do mention it in the comment section using the contact form or you can send the details to our official mail id i.e. firstname.lastname@example.org. We will be happy to assist you.
Thank you. Keep exploring!