ImportError cannot import name escape & Markup from jinja2

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
6 min

banner

# Table of Contents

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

Note: If you got the error "ImportError: cannot import name 'Markup' from 'jinja2'", click on the second subheading.

# ImportError: cannot import name 'escape' from 'jinja2'

The "ImportError: cannot import name 'escape' from 'jinja2'" is caused because the escape function was removed in version 3.1.0 of jinja2.

To solve the error, upgrade your version of Flask or correct your import statements to import escape from markupsafe instead.

importerror cannot import name escape from jinja2

shell
ImportError: cannot import name 'escape' from 'jinja2' (/home/borislav/Desktop/bobbyhadz_python/venv/lib/python3.11/site-packages/jinja2/__init__.py)

As shown in this section of the docs, the escape method has been removed in version 3.1.0 of Jinja2 and should now be imported from the markupsafe package.

# If you use Flask, update your version

Upgrade your version of Flask if you use the package.

Older versions of Flask use the escape method under the hood.

shell
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

upgrade flask version

When you run the command, you might get an error that states "ERROR: pip's dependency resolver does not currently take into account all the packages that are installed.".

However, even though the error is shown, the package is installed successfully.

Starting with Flask version 2, the import statements in the package have been updated to import the escape method from markupsafe instead of jinja2.

# Installing an older version of jinja to solve the error

An alternative way to solve the error is to downgrade your version of jinja2 to 3.0.3 as that is the last jinja2 version that exports the escape method.

shell
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

install older version of jinja2

The --force-reinstall option forces pip to reinstall the package.

If you have a requirements.txt file, you can add the following line.

requirements.txt
jinja2==3.0.3

You can use the pip show jinja2 command to check which version of the package is installed.

shell
pip show jinja2 pip3 show jinja2 python -m pip show jinja2 python3 -m pip show jinja2

pin jinja2 version

Alternatively, you can import the escape method from the markupsafe module and stick with the recent versions of jinja2.

# Update your import statements to import escape from markupsafe

If the error occurred in your code, import the escape method from the markupsafe module.

main.py
from markupsafe import escape value = escape("<p>bobbyhadz</p>") print(escape(value)) # ๐Ÿ‘‰๏ธ &lt;p&gt;bobbyhadz&lt;/p&gt; # ๐Ÿ‘‡๏ธ &lt;p&gt;bobbyhadz&lt;/p&gt; &lt;p&gt;.com&lt;/p&gt; print(value + " <p>.com</p>")

import escape from markupsafe

Make sure to remove all occurrences of the following import statement from your code.

main.py
# โ›”๏ธ Old import style (Jinja2 < 3.1.0) from jinja2 import escape

You might have to upgrade your jinja2 and markupsafe versions if you installed an older version prior.

shell
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

If none of the suggestions helped, try upgrading the version of all of your packages in the environment.

If your error is caused by having a package that imports escape from jinja2, you have to upgrade the package by running the pip install package_name --upgrade command.

# Upgrade all packages in your environment

The most straightforward way to upgrade all outdated packages is to use a Python script.

main.py
import pkg_resources from subprocess import call packages = [dist.project_name for dist in pkg_resources.working_set] call("pip install --upgrade " + ' '.join(packages), shell=True)
You can store the script in a Python file, e.g. main.py and run the file with python main.py to upgrade all of the outdated packages.

Here are alternative commands you can use to upgrade all outdated packages.

shell
# ๐Ÿ‘‡๏ธ macOS or Linux pip install -U `pip list --outdated | awk 'NR>2 {print $1}'` # ๐Ÿ‘‡๏ธ Windows for /F "delims= " %i in ('pip list --outdated') do pip install -U %i

If you use a requirements.txt file, you can update it with the following command.

shell
pip freeze > requirements.txt

If the error persists, follow the instructions in my ModuleNotFoundError: No module named 'flask' article.

# ImportError: cannot import name 'Markup' from 'jinja2'

The "ImportError: cannot import name 'Markup' from 'jinja2'" is caused because the Markup class was removed in version 3.1.0 of jinja2.

To solve the error, upgrade your version of Flask or correct your import statements to import Markup from markupsafe instead.

importerror cannot import name markup from jinja2

shell
ImportError: cannot import name 'Markup' from 'jinja2' (/home/borislav/Desktop/bobbyhadz_python/venv/lib/python3.11/site-packages/jinja2/__init__.py)

As shown in this section of the docs, the Markup class has been removed in version 3.1.0 of Jinja2 and should now be imported from the markupsafe package.

# Upgrade your version of Flask if you use the package

The first thing you should try is to upgrade your version of Flask if you use the package as older versions of Flask use the Markup class under the hood.

shell
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

update flask version

When you run the command, you might get an error that states "ERROR: pip's dependency resolver does not currently take into account all the packages that are installed.".

However, even though the error is shown, the package is installed successfully.

Starting with Flask version 2, the import statements in the package have been updated to import the Markup class from markupsafe instead of jinja2.

# The error might also be caused by another package

If your error is caused by having a package that imports Markup from jinja2, you have to upgrade the package by running the pip install <package_name> --upgrade command.

shell
pip install <package_name> --upgrade

# Pinning your version of jinja2 to 3.0.3

An alternative way to solve the error is to downgrade your version of jinja2 to 3.0.3 as that is the last jinja2 version that exports the Markup class.

shell
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

pin jinja2 version

The --force-reinstall option forces pip to reinstall the package.

If you have a requirements.txt file, you can add the following line.

requirements.txt
jinja2==3.0.3

The line will pin the version of the jinja2 package to 3.0.3 which is the last version that exports the Markup class.

# Checking which version of the package is installed

You can use the pip show jinja2 command to check which version of the package is installed.

shell
pip show jinja2 pip3 show jinja2 python -m pip show jinja2 python3 -m pip show jinja2

verify which version is installed

Alternatively, you can import the Markup class from the markupsafe module and stick with the recent versions of jinja2.

# Update your import statements to import Markup from markupsafe

If the error occurred in your code, import the Markup class from the markupsafe module.

main.py
from markupsafe import Markup value = Markup('bobby, <b>hadz</b>!') print(value) # ๐Ÿ‘‰๏ธ bobby, <b>hadz</b>! print(Markup(123)) # ๐Ÿ‘‰๏ธ 123 # ๐Ÿ‘‡๏ธ bobby, &lt;b&gt;hadz&lt;/b&gt;! print(Markup.escape('bobby, <b>hadz</b>!'))

import markup from markupsafe

Make sure to remove all occurrences of the following import statement from your code.

main.py
# โ›”๏ธ Old import style (Jinja2 < 3.1.0) from jinja2 import Markup

You might have to upgrade your jinja2 and markupsafe versions if you installed an older version prior.

shell
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

If none of the suggestions helped, try upgrading the version of all of your packages in the environment.

# Upgrade all packages in your environment

The most straightforward way to upgrade all outdated packages is to use a Python script.

main.py
import pkg_resources from subprocess import call packages = [dist.project_name for dist in pkg_resources.working_set] call("pip install --upgrade " + ' '.join(packages), shell=True)
You can store the script in a Python file, e.g. main.py and run the file with python main.py to upgrade all of the outdated packages.

Here are alternative commands you can use to upgrade all outdated packages.

shell
# ๐Ÿ‘‡๏ธ macOS or Linux pip install -U `pip list --outdated | awk 'NR>2 {print $1}'` # ๐Ÿ‘‡๏ธ Windows for /F "delims= " %i in ('pip list --outdated') do pip install -U %i

If you use a requirements.txt file, you can update it with the following command.

shell
pip freeze > requirements.txt

If the error persists, follow the instructions in the following articles:

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev