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

The “ImportError: cannot import name ‘escape’ from ‘jinja2’” because the ‘escape’ function was removed in version 3.1.0 of ‘jinja2‘. The solution to this error is to upgrade your version of ‘Flask‘ or change your import statements to import ‘escape’ from ‘markupsafe‘ instead of ‘escape‘ from ‘jinja2‘.

According to this section of the document, the ‘escape‘ method has been removed in version 3.1.0 of ‘Jinja2‘ and should now be imported from the ‘markupsafe‘ package.

Solution: cannot import name ‘escape’ from ‘jinja2’

The first thing you can do is upgrade your ‘flask‘ version if you are using an older version of the package.

To resolve the issue of cannot import name escape from Jinja2, try the following commands:

pip install Flask --upgrade
pip3 install Flask --upgrade

python -m pip install Flask --upgrade
python3 -m pip install Flask --upgrade
py -m pip install Flask --upgrade

# for Jupyter Notebook
!pip install Flask --upgrade

When you run the command, you may face the error “ERROR: pip’s dependency resolver does not currently take into account all the packages that are installed.

If you got this error while upgrading the package, don’t worry. You have successfully upgraded the packages.

Fixed: cannot import name escape from jinja2

You can also solve this problem by downgrading the ‘jinja2‘ version to 3.0.3. Jinja2 version 3.0.3 is the last version that supports the ‘escape‘ method. Then you can use it as “from jinja2 import escape“.

Solved: ImportError: cannot import name ‘markup’ from ‘jinja2’.

Try these commands for the “cannot import name escape from Jinja2” solution:

pip install jinja2==3.0.3 --force-reinstall
pip3 install jinja2==3.0.3 --force-reinstall

# if you don't have pip in PATH environment variable
python -m pip install jinja2==3.0.3 --force-reinstall
python3 -m pip install jinja2==3.0.3 --force-reinstall

# py alias (Windows)
py -m pip install jinja2==3.0.3 --force-reinstall
# for Jupyter Notebook
!pip install jinja2==3.0.3 --force-reinstall

If you are using the requirements.txt file for installing packages, you can add this line to your requirements.txt file.

jinja2==3.0.3

If you want to check the current version of the ‘jinja2‘ package. You can use these commands.

pip show jinja2
pip3 show jinja2

python -m pip show jinja2
python3 -m pip show jinja2

Fixed: modulenotfounderror: no module named ‘jinja2’

If you are facing this error: “modulenotfounderror: no module named ‘jinja2’” you can run this command to solve this error.

# modulenotfounderror: no module named 'jinja2' 
pip install jinja2

Solved: importerror: cannot import name ‘escape’ from ‘jinja2’

Another method to solve this problem is to “update your import statements to import escape from markupsafe“.

Your new code should look like this one.

from markupsafe import escape
value = escape("<p>freetoreads.com</p>")
print(escape(value))

The old style to import escape was like this:

# old import style (Jinja2 < 3.1.0)
from jinja2 import escape

If you are using the older version of ‘markupsafe‘ upgrade your package version using these commands.

pip install jinja2 markupsafe --upgrade
pip3 install jinja2 markupsafe --upgrade

python -m pip install jinja2 markupsafe --upgrade
python3 -m pip install jinja2 markupsafe --upgrade
py -m pip install jinja2 markupsafe --upgrade

# for Jupyter Notebook
!pip install jinja2 markupsafe --upgrade

Conclusion on importerror: cannot import name ‘escape’ from ‘jinja2’:

The “ImportError: cannot import name ‘escape’ from ‘jinja2’” because the ‘escape’ function was removed in version 3.1.0 of ‘jinja2’. To solve the error, upgrade your version of Flask or change your import statements to import escape from markupsafe instead.

Leave a Reply