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

Being a developer, you may have come across Importerror: cannot import name ‘escape’ from ‘jinja2’ error at some point.

This trouble encounters when importing the escape function from the Jinja2 module in your Python project.

In this article, we will explain the causes of the error, and provide solutions to help you resolve the issue.

What is Importerror: cannot import name ‘escape’ from ‘jinja2’?

The ImportError: cannot import name ‘escape’ from jinja2 occurs when you try to import the escape function from the jinja2 module. It is because the escape function was removed from the Jinja2 module in version 3.1.0.

Additionally, the escape’ function is a utility function in the Jinja2 templating engine that helps to prevent cross-site scripting (XSS) attacks by escaping special characters in user-generated content.

Let’s say you import the following statement in your code:

from jinja2 import escape

template = '<h1>{{ data }}</h1>'
content = {'data': 'Welcome'}
result = render_template_string(template, content)

print(result)

When we run the code along with the latest Jinja2 module, Python will give the following error:

Traceback (most recent call last):
File …
from jinja2 import escape
ImportError: cannot import name 'escape' from 'jinja2'

So why does this error occur asides from the mentioned above?

Why did this error occur?

Certainly, the possible reasons why we get this error include the following:

  • Incorrect installation of the Jinja2 package.
  • Conflicting versions of the Jinja2 package.
  • The ‘escape’ function may have been removed or renamed in a newer version of Jinja2.

This time we will fix this error with the solution provided in the next section…

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

1. Correct the import statement

In order to fix the error, we need to refactor your import statement as follows:

from markupsafe import escape

Since the MarkUpSafe module is already present as a dependency of the Jinja2 module, therefore to install the module separately we don’t need to use pip.

2. Downgrade your Jinja2 version

Alternatively, we can still use jinja2 but we need to downgrade the Jinja2 module to its latest version wherein it still supports the function.

So the latest version that still includes the escape function is jinja2 version 3.0.3. Anyway, we need to use pip to downgrade this module.

The command is as follows:

pip install jinja2==3.0.3 --force-reinstall

📌 For pip3 here’s the command:


pip3 install jinja2==3.0.3 --force-reinstall

3. Upgrade to Flask version 2

When you get this error while you are using the Flask framework, then this error happens because Flask version 1 used the escape function from Jinja2 in its code.

The thing we need to do is to upgrade the Flask module to the latest version using pip.

Use one of the commands below in your terminal:

pip install flask --upgrade

📌 For pip3 here is the command:

pip3 install flask --upgrade

Definitely, the latest Flask version should fix this issue, so you have no problem running your application

In addition to this if you have requirements.txt file, just instruct pip to install an explicit version of Jinja2 as follows:

jinja2==3.0.3

Alternatively, this is a good solution so you don’t have to install Jinja2 version immediately.

I think that’s all for the solution…

Anyway here are some other fixed errors wherein you can refer to try when you might face these errors:

Conclusion

In conclusion, Importerror: cannot import name ‘escape’ from ‘jinja2’ can be fixed by refactoring the import statement and downgrading the Jinja2 version.

Additionally, those who are using the Flask, it need also need to upgrade the Flask module in order to utilize the latest Jinja2 version.

I think that’s all for this article. I hope it helped you fix the error.

Until next time! 😊

Frequently Asked Questions

What is Python ImportError and what causes it?

ImportError is raised when an import fails for any reason. The most specific subtype is ModuleNotFoundError (no such module). Plain ImportError typically means the module exists but a name inside it can’t be imported, e.g. ‘cannot import name X from Y’ (X was renamed, removed, or moved between versions of Y). Common with library version mismatches.

How do I fix ‘cannot import name X from Y’?

Three steps: (1) Check the library version: pip show Y. (2) Check the changelog of Y, X may have been renamed or removed in a recent release. (3) Either pin to an older Y version (pip install Y==1.x.y) or update your code to the new import path. Common 2025-2026 examples: Werkzeug url_decode removed, Pillow ANTIALIAS renamed to LANCZOS.

Why does the import work in REPL but fail in script?

Two reasons. (1) Different Python interpreter: REPL uses one Python, your script uses another. Run python –version both times. (2) Different working directory: REPL is started where you have access to local modules, script is run from a different cwd. Add the project path to sys.path or use python -m to run as a module.

How do I avoid circular import errors?

Circular imports happen when module A imports B and B imports A at the top level. Three fixes: (1) Move one import inside the function that uses it (lazy import). (2) Restructure code so A and B both import from a third module C. (3) Use TYPE_CHECKING for type-hint-only imports: if TYPE_CHECKING: from a import X.

Where can I find more ImportError fixes?

Browse the ImportError reference hub for 67+ specific fixes (Flask, Werkzeug, Django, ML library versions). For missing-module cases see ModuleNotFoundError. For Python setup help see Python Tutorial hub.

Glay Eliver

Programmer & Technical Writer at PIES IT Solution

Glay Eliver is a programmer and writer at PIES IT Solution, author of over 600 tutorials at itsourcecode.com. Specializes in JavaScript tutorials, Microsoft Office how-tos (Excel, Word, PowerPoint), and Python error debugging covering ImportError, TypeError, AttributeError, ModuleNotFoundError, and JavaScript ReferenceError. Authored several of the site’s highest-traffic Excel and MS Office reference articles.

Expertise: JavaScript · MS Excel · MS Word · MS PowerPoint · Python · Python ImportError · Python TypeError · Python AttributeError · ModuleNotFoundError · JavaScript ReferenceError · Pygame  · View all posts by Glay Eliver →