Find every failing import by searching your codebase for imports of Markup or escape from jinja2. Expected: one or more lines matching from jinja2 import Markup or escape.

Change those lines to import from markupsafe instead, for example from markupsafe import Markup, escape. Expected: the import statement points at markupsafe and no jinja2 import of those names remains.

Check third party packages if the traceback points inside site-packages rather than your code. Expected: the failing package and its version are identified in the traceback.

Upgrade that third party package to a release that supports Jinja2 3.1, or pin it to a compatible version. Expected: pip shows a newer compatible version installed.

Reinstall and verify versions with pip show jinja2 markupsafe. Expected: Jinja2 is 3.1 or later and markupsafe is installed alongside it.

Restart the app and import the module that failed. Expected: the import succeeds with no ImportError and the app starts.

Run your test suite or render one template that uses Markup. Expected: templates render and marked strings are not double escaped.