# ImportError: cannot import name 'Markup' from 'jinja2' (or 'escape'): how to fix it

> ImportError: cannot import name 'Markup' from 'jinja2', and the same error for 'escape', means something imported those names from Jinja2 while Jinja2 3.1 or later is installed. Jinja2 3.1.0 removed them; they live in MarkupSafe. The usual culprit is an old Flask 1.x pinned in requirements.txt next to an unpinned Jinja2. Upgrade Flask or the package the traceback names, import Markup and escape from markupsafe in your own code, or pin the old versions as a stopgap.

Source: https://livops.dev/en/guides/error-jinja2-cannot-import-name-markup

By Livops team, the people who build and run Livops. Updated 4 December 2026.

The app ran yesterday, and after a fresh deploy it stops before serving a single request. With Flask 1.1.2 in `requirements.txt` the traceback ends inside Flask itself[4]:

```text
  File ".../site-packages/flask/__init__.py", line 14, in <module>
    from jinja2 import escape
ImportError: cannot import name 'escape' from 'jinja2' (.../site-packages/jinja2/__init__.py)
```

With Flask 1.0.x, another old package or your own code, the same error can name `Markup`: `ImportError: cannot import name 'Markup' from 'jinja2'`. This page explains which Jinja2 release removed both names, how to tell from the traceback which package still imports them, and the fixes: upgrade that package, import from MarkupSafe in your own code, or pin the old versions while you upgrade.

## What does the error mean?

Python raises `ImportError` when the "from list" in `from ... import` has a name that cannot be found[21]. Here the module is `jinja2`, the template engine Flask uses, and the names are `Markup` and `escape`.

Those two never belonged to Jinja2: they come from MarkupSafe, the library that escapes text so it is safe to use in HTML[8]. Jinja2 re-exported them for a long time. Jinja2 3.0 kept both names but made them warn: `'jinja2.Markup' is deprecated and will be removed in Jinja 3.1. Import 'markupsafe.Markup' instead.`, and the same for `escape`[2]. Jinja2 3.1.0, released on 24 March 2022, removed previously deprecated code, and its changelog says `Markup` and `escape` should be imported from MarkupSafe[1]. From 3.1.0 on, `from jinja2 import Markup` fails. The current Jinja2 on PyPI is 3.1.6[17], so any install that takes the newest Jinja2 gets the error.

### Why Flask 1.x is the usual cause

Flask 1.1.2 imports `escape` from `jinja2` on line 14 of `flask/__init__.py`, and `Markup` on line 15[4]. Python stops at the first name it cannot find, which is why Flask 1.1.x fails on `escape`. Flask 1.0.4 has `from jinja2 import Markup, escape` in one line[23], so it fails on `Markup` instead. Flask 1.1.2 asks for `Jinja2>=2.10.1`, with no upper limit[5], so pip is free to install Jinja2 3.1.

Flask 1.1.3 set maximum versions for Werkzeug, Jinja, Click and ItsDangerous[3]: Flask 1.1.4 asks for `Jinja2 >= 2.10.1, < 3.0`[6], so pip does not pair it with Jinja2 3.1. Flask 2.0.0 requires Jinja2 3 or newer[3] and imports `escape` and `Markup` from `markupsafe`[7]. In short, the error comes from Flask 1.1.2 or older next to a Jinja2 that nobody pinned.

### The same removals, other names

The same clean-ups in the Pallets libraries give errors of the same shape. Use the name in the message to find the row:

| The error says | Where it usually comes from | Fix |
| --- | --- | --- |
| `cannot import name 'escape' from 'jinja2'` | Flask 1.1.2 or older at start[4] | Upgrade Flask, or pin Flask 1.1.4 (below) |
| `cannot import name 'Markup' from 'jinja2'` | Flask 1.0.x at start[23], another old package, or your own code | `from markupsafe import Markup`[1] |
| `cannot import name 'contextfilter' from 'jinja2'` | a custom filter written for Jinja2 2.x | `pass_context`; Jinja2 3.1.0 removed `contextfilter`, `contextfunction` and the other `*filter`/`*function` decorators[1] |
| `cannot import name 'Markup' from 'flask'` | Flask 3 with code or an extension that imported `Markup` or `escape` from `flask` | `from markupsafe import Markup`; Flask 2.3.0 deprecated those imports[3], and Flask 3.0.0 no longer has them[14]. Flask-WTF fixed its `flask.Markup` import in 1.1.2[15] |
| `cannot import name 'soft_unicode' from 'markupsafe'` | Jinja2 2.11 with MarkupSafe 2.1 or newer | pin `markupsafe<2.1`, or upgrade Jinja2; Jinja2 2.11.3 imports `soft_unicode`[10], and MarkupSafe 2.1.0 removed it[9] |
| `cannot import name 'json' from 'itsdangerous'` | Flask 1.1.2 or older with ItsDangerous 2.1 or newer | upgrade Flask; Flask 1.1.2 imports `json` from `itsdangerous`[13], and ItsDangerous 2.1.0 removed it[12] |

The last two rows matter for the stopgap below: an old Flask has more than one dependency that moved on.

## Find which package does the import

The traceback tells you. Look at the `File` line just above `ImportError`:

- **A path through `site-packages`** is an installed package. The folder right after `site-packages` is its name: `.../site-packages/flask/__init__.py` is Flask.
- **A path inside your project** is your own code. Change the import (Fix 2).

Then see which version of that package is installed, and what requires it:

```sh
.venv/bin/python -m pip show flask
```

pip prints the package's `Version`, its `Requires` and its `Required-by` lines[22]. If the package was pulled in by another one, `Required-by` names that one, and that is the one to upgrade. To list every installed file that still imports these names from Jinja2 at once:

```sh
grep -rnE "from jinja2 import .*(Markup|escape)" .venv/lib/python3*/site-packages
```

## Why it breaks on deploy and not locally

Two installs of the same `requirements.txt` end differently:

- **Your laptop** installed Jinja2 when the project started, before 3.1 existed or before you last changed anything. `pip install` prefers to leave the installed version as it is unless `--upgrade` is given[18], so running it again keeps the old Jinja2.
- **A fresh environment** has nothing installed. pip starts from the most recent version of a package[19], so with `flask==1.1.2` it takes the newest Jinja2 that Flask's `>=2.10.1` allows[5]: a 3.1 release.

Pinning protects you from incompatibilities in newly released versions[20]. A `requirements.txt` that lists `flask==1.1.2` and leaves Jinja2 out pins Flask and nothing under it.

## Fix 1: upgrade Flask, or the package that imports it

The lasting fix is a version of the package that imports from MarkupSafe. For Flask that means 2.0 or later[7]; the current Flask on PyPI is 3.1.3[16]:

```text
flask>=3.1
```

Plan for more than a version bump. Flask 2.0.0 dropped Python 2 and raised the minimum versions of Werkzeug, Jinja2, MarkupSafe, ItsDangerous and Click, and its changelog asks you to check each project's change log[3]. Flask 2.3.0 and 3.0.0 each removed code that was deprecated before[3]. Upgrade in a branch, update your Flask extensions too, run the app and its tests from a new virtual environment, and deploy when that starts clean. If the upgrade leaves you with `cannot import name 'url_quote' from 'werkzeug.urls'`, see [that error's page](https://livops.dev/en/guides/error-flask-cannot-import-name-url-quote).

When the traceback named another package, upgrade that one the same way, and check its changelog for the first release that supports Jinja2 3.1.

## Fix 2: import Markup and escape from MarkupSafe in your own code

If the `File` line points into your project, change the import:

```python
# before
from jinja2 import Markup, escape

# after
from markupsafe import Markup, escape
```

That is where the names live: `escape()` escapes text and returns a `Markup` object[8]. MarkupSafe is already installed, since Jinja2 depends on it[17]. If your code uses `from flask import Markup` or `from flask import escape`, change it the same way: Flask 3.0.0 no longer exports them[14].

## Fix 3 (stopgap): pin the old versions

When the site has to be back up today and the upgrade needs testing, pin versions that still fit together.

**For an old Flask**, pinning Jinja2 alone is not enough: Flask 1.1.2 would then stop on `itsdangerous`[13][12]. Move to Flask 1.1.4 instead, which caps its own dependencies at `Werkzeug < 2.0`, `Jinja2 < 3.0`, `itsdangerous < 2.0` and `click < 8.0`[6], and pin MarkupSafe, because the Jinja2 2.11 that this installs asks only for `MarkupSafe>=0.23`[11] and imports `soft_unicode`, which MarkupSafe 2.1.0 removed[10][9]:

```text
flask==1.1.4
markupsafe<2.1
```

The 1.x line has had no release since 1.1.4, in May 2021[3], so treat this as a bridge to Fix 1, and test it locally on the same Python you deploy with.

**For another package**, pin Jinja2 below 3.1. Jinja2 3.0 still provides `jinja2.Markup` and `jinja2.escape`, with a `DeprecationWarning`[2]:

```text
jinja2<3.1
```

The warnings are a reminder that this pin has an end date: the package that needs it has to be upgraded or replaced.

## How to reproduce it locally

The error hides on your laptop because the old Jinja2 is still installed. Do what the server does, from nothing:

```sh
python3 -m venv /tmp/fresh-venv
/tmp/fresh-venv/bin/python -m pip install -r requirements.txt
/tmp/fresh-venv/bin/python -m pip show jinja2
/tmp/fresh-venv/bin/python -c "import app"
```

If `pip show` prints a 3.1 version and the import fails with the same `ImportError`, you are seeing what the server sees. After a fix, delete `/tmp/fresh-venv`, run the same lines again, and the import should finish without an error.

## On Livops

- **Dependencies are installed on the server.** Livops installs your `requirements.txt` itself on each build, much like the fresh environment above, so an unpinned Jinja2 can resolve to a newer version on any deploy. Pin it in `requirements.txt` and the next deploy installs that version.
- **Do not upload your `.venv`.** When you upload a folder, `venv` and `.venv` are dropped automatically; we install dependencies ourselves.
- **A failed start.** This error stops the app as it starts, so the deploy fails as `start_failed`, and a web app's previous version keeps running. The traceback is on the **Logs** page, and the [assistant](https://livops.dev/en/docs/assistant) can read the log and explain what happened. See [The app did not start](https://livops.dev/en/docs/error-start).
- **Python commands use the project's environment.** A start command of your own goes in `livops.json` as `"start"` and uses the project's `.venv`, for example `.venv/bin/python app.py`. More in [Flask hosting](https://livops.dev/en/hosting/flask-hosting).
- **Choose the Python.** Livops runs Python 3.10 to 3.14. A `.python-version` file at the project root pins it:

  ```text
  3.12
  ```

A Python web app costs **39 credits a month**. When the error is `No module named` rather than `cannot import name`, the package is not installed at all: see [ModuleNotFoundError: No module named](https://livops.dev/en/guides/error-module-not-found-python).

## Questions

**Which Jinja2 version removed Markup and escape?**

Jinja2 3.1.0, released on 24 March 2022. Jinja2 3.0 still had both names, with a deprecation warning saying they would be removed in 3.1 and should be imported from MarkupSafe. Every Jinja2 from 3.1.0 on raises this ImportError.

**Why does the error name escape when I searched for Markup?**

It depends on the order of the imports. Flask 1.1.x imports escape from jinja2 on line 14 of flask/__init__.py and Markup on the line after it, so it fails on escape. Flask 1.0.4 imports Markup, escape in one line, so it fails on Markup. Another old package or your own code can fail on either.

**Is pinning Jinja2 below 3.1 enough?**

Only when the package that imports Markup or escape works with the rest of your current dependencies. Flask 1.1.2 is not such a package: after Jinja2 it fails on json from itsdangerous, which itsdangerous 2.1.0 removed. For an old Flask, pin Flask 1.1.4, which caps its own dependencies, plus MarkupSafe below 2.1.

**What should my own code import instead?**

from markupsafe import Markup, escape. Jinja2's changelog says Markup and escape should be imported from MarkupSafe, and Flask 2.0 and later import them from there too. Do not switch to from flask import Markup: Flask 2.3 deprecated that and Flask 3.0 removed it.

**Why does it work on my laptop and fail on the server?**

Your laptop still has the Jinja2 that was installed when the project started, and pip leaves an installed version alone unless you pass --upgrade. The server installs from scratch, and Flask 1.1.2 asks only for Jinja2 2.10.1 or newer, so pip takes the newest Jinja2.

**How do I find which package does the import?**

Read the File line just above the ImportError in the traceback. If the path goes through site-packages, the folder after site-packages is the package, for example flask; if the path is in your project, it is your own code. Then pip show with that name prints its installed version.

**Which Python versions does Livops run?**

Python 3.10 to 3.14. Put the version in a .python-version file at the project root, for example 3.12.

## Sources

1. [Jinja — Changes](https://jinja.palletsprojects.com/en/stable/changes/). Checked 4 December 2026.
2. [Jinja source — src/jinja2/utils.py (3.0.3)](https://github.com/pallets/jinja/blob/3.0.3/src/jinja2/utils.py). Checked 4 December 2026.
3. [Flask — Changes](https://flask.palletsprojects.com/en/stable/changes/). Checked 4 December 2026.
4. [Flask source — src/flask/__init__.py (1.1.2)](https://github.com/pallets/flask/blob/1.1.2/src/flask/__init__.py). Checked 4 December 2026.
5. [Flask source — setup.py (1.1.2)](https://github.com/pallets/flask/blob/1.1.2/setup.py). Checked 4 December 2026.
6. [Flask source — setup.py (1.1.4)](https://github.com/pallets/flask/blob/1.1.4/setup.py). Checked 4 December 2026.
7. [Flask source — src/flask/__init__.py (2.0.0)](https://github.com/pallets/flask/blob/2.0.0/src/flask/__init__.py). Checked 4 December 2026.
8. [MarkupSafe documentation](https://markupsafe.palletsprojects.com/en/stable/). Checked 4 December 2026.
9. [MarkupSafe — Changes](https://markupsafe.palletsprojects.com/en/stable/changes/). Checked 4 December 2026.
10. [Jinja source — src/jinja2/filters.py (2.11.3)](https://github.com/pallets/jinja/blob/2.11.3/src/jinja2/filters.py). Checked 4 December 2026.
11. [Jinja source — setup.py (2.11.3)](https://github.com/pallets/jinja/blob/2.11.3/setup.py). Checked 4 December 2026.
12. [ItsDangerous — Changes](https://itsdangerous.palletsprojects.com/en/stable/changes/). Checked 4 December 2026.
13. [Flask source — src/flask/json/__init__.py (1.1.2)](https://github.com/pallets/flask/blob/1.1.2/src/flask/json/__init__.py). Checked 4 December 2026.
14. [Flask source — src/flask/__init__.py (3.0.0)](https://github.com/pallets/flask/blob/3.0.0/src/flask/__init__.py). Checked 4 December 2026.
15. [Flask-WTF — Changes](https://flask-wtf.readthedocs.io/en/latest/changes/). Checked 4 December 2026.
16. [PyPI — Flask](https://pypi.org/project/Flask/). Checked 4 December 2026.
17. [PyPI — Jinja2](https://pypi.org/project/Jinja2/). Checked 4 December 2026.
18. [pip docs — pip install](https://pip.pypa.io/en/stable/cli/pip_install/). Checked 4 December 2026.
19. [pip docs — Dependency Resolution](https://pip.pypa.io/en/stable/topics/dependency-resolution/). Checked 4 December 2026.
20. [pip docs — Repeatable Installs](https://pip.pypa.io/en/stable/topics/repeatable-installs/). Checked 4 December 2026.
21. [Python docs — Built-in Exceptions](https://docs.python.org/3/library/exceptions.html). Checked 4 December 2026.
22. [pip source — src/pip/_internal/commands/show.py](https://github.com/pypa/pip/blob/main/src/pip/_internal/commands/show.py). Checked 4 December 2026.
23. [Flask source — flask/__init__.py (1.0.4)](https://github.com/pallets/flask/blob/1.0.4/flask/__init__.py). Checked 4 December 2026.
