Pip is any Python programmer’s best friend. The package manager makes it easy to install just about any Python package by condensing the process down to a single command.
However, Pip isn’t immune to random bugs and glitches that can throw a stick in your workflow from time to time. In this article, we’re taking a look at the ‘cannot import name markup from Jinja2’ error.
Also read:Â How to flatten a list and list of lists in Python?
Why does it happen?
The error itself denotes that either the Jinja2 package hasn’t been installed properly or that your specific Python installation can’t locate the package. Another possible reason for this could be an incompatible or no installation of Flask, a prerequisite for Jinja2.
How to fix this?
Here are three fixes you can try out.
Import Markup the right way
Jinja 2 might seem like a usual Python package and for the most part it works as such. However, if the usual import statement for Markup isn’t working, you can try importing it as shown below.
from jinja2.utils import markupsafe
markupsafe.Markup()
Markup('')
This should resolve the import error and get the package running just fine.
Install the right version of Flask and Jinja2
Another rather easy solution to the problem is ensuring you have the right version of Flask and Jinja2. You need the following versions or above for both packages to work fine.
- Flask: Version 2.0.3
- Jinja2: Version 3.1.1
Installing both of them is rather easy thanks to Pip. Just enter the following commands one at a time and you’re good to go.
pip install Flask==2.0.3
pip install Jinja2==3.1.1

Once the installations are complete, try importing Markup from Jinja2 again and it should work without any problems.
Update all packages
If you’re making a Flask-based app, it’s also a good idea to make sure all required packages are up to date. You can do so using the following Pip command.
pip install --upgrade [package name]

Also read: How to solve the Tower of Hanoi problem using Python?