Last updated: Apr 10, 2024
Reading timeยท6 min

Note: If you got the error "ImportError: cannot import name 'Markup' from 'jinja2'", click on the second subheading.
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' (/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.
Upgrade your version of Flask if you use the package.
Older versions of Flask use the escape
method under the hood.
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 might get an error that states "ERROR: pip's dependency resolver does not currently take into account all the packages that are installed.".
Starting with Flask version 2, the import statements in the package have been
updated to import the escape method from markupsafe instead of jinja2.
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.
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

The --force-reinstall option
forces pip to reinstall the package.
If you have a requirements.txt file, you can add the following line.
jinja2==3.0.3
You can use the pip show jinja2 command to check which version of the package
is installed.
pip show jinja2 pip3 show jinja2 python -m pip show jinja2 python3 -m pip show jinja2

Alternatively, you can import the escape method from the markupsafe module
and stick with the recent versions of jinja2.
escape from markupsafeIf the error occurred in your code, import the escape method from the
markupsafe module.
from markupsafe import escape value = escape("<p>bobbyhadz</p>") print(escape(value)) # ๐๏ธ <p>bobbyhadz</p> # ๐๏ธ <p>bobbyhadz</p> <p>.com</p> print(value + " <p>.com</p>")

Make sure to remove all occurrences of the following import statement from your code.
# โ๏ธ 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.
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.
The most straightforward way to upgrade all outdated packages is to use a Python script.
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)
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.
# ๐๏ธ 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.
pip freeze > requirements.txt
If the error persists, follow the instructions in my ModuleNotFoundError: No module named 'flask' article.
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' (/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.
Flask if you use the packageThe 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.
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 might get an error that states "ERROR: pip's dependency resolver does not currently take into account all the packages that are installed.".
Starting with Flask version 2, the import statements in the package have been
updated to import the Markup class from markupsafe instead of jinja2.
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.
pip install <package_name> --upgrade
3.0.3An 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.
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

The --force-reinstall option
forces pip to reinstall the package.
If you have a requirements.txt file, you can add the following line.
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.
You can use the pip show jinja2 command to check which version of the package
is installed.
pip show jinja2 pip3 show jinja2 python -m pip show jinja2 python3 -m pip show jinja2

Alternatively, you can import the Markup class from the markupsafe module
and stick with the recent versions of jinja2.
Markup from markupsafeIf the error occurred in your code, import the Markup class from the
markupsafe module.
from markupsafe import Markup value = Markup('bobby, <b>hadz</b>!') print(value) # ๐๏ธ bobby, <b>hadz</b>! print(Markup(123)) # ๐๏ธ 123 # ๐๏ธ bobby, <b>hadz</b>! print(Markup.escape('bobby, <b>hadz</b>!'))

Make sure to remove all occurrences of the following import statement from your code.
# โ๏ธ 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.
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.
The most straightforward way to upgrade all outdated packages is to use a Python script.
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)
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.
# ๐๏ธ 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.
pip freeze > requirements.txt
If the error persists, follow the instructions in the following articles:
You can learn more about the related topics by checking out the following tutorials: