chevron_left
1,680 points
10 5 2

Importerror: cannot import name 'markup' from 'jinja2'
Photo by Larisa-K

I like Python because it is an easy language and provide a wide range of solutions via its modules. Error is a common issue in any programming language, the reason for the error would be different. ImportError: cannot import name ‘markup’ from ‘jinja2’ clear form the message is an import error. The user can get rid of this error by upgrading the module or switching back to an older version of the module.

ImportError: cannot import name ‘markup’ from ‘jinja2’, the Error description  #

A user may face an Import error while importing a module in the Python programming language. The same is clear here, the error occurs while importing the module of jinja2. It is a templating module launched after Django and also a templating module in Python used to markup HTML, XML, or other markup types that the user receives in response to an HTTP request. When a Python user imports jinja2, he may face this error. The reasons are different which I will explain in the below section of the article.

What brings the error, the Reasons  #

The reasons for the error are different and multiple. One is the user is importing the jinja2 module without installing it or when it was installed there was some error or it was not installed completely. So when the user will import the jinja2 module and its installation was incomplete, the user will encounter the import error.

Information: The ImportError can occur under two circumstances. if the module is missing. Or  if we're going to import a submodule of main module.

There may be a reason that the flask package is not installed on your python or python on your machine is not able to locate the jinja2 module because flask is also not installed.

As I said the reason may be different according to the scenario, another cause that may create this annoying scenario for the user is importing the jinja2 module in the wrong way which is called syntax error. If the user will not use the correct phrase to import the jinja2 module then he may encounter the ImportError: cannot import name ‘markup’ from ‘jinja2’.

The flask version and jinja2 module version also should match, a user may experience the ImportError: cannot import name ‘markup’ from ‘jinja2’ if the version of both packages will not be compatible with each other.

In short, I can say, the error itself indicates either improper installation of the Jinja2 package or an inability of your particular Python system to locate the package. Another explanation for this can be that Flask, a necessary component for Jinja2, is either not installed or is incompatible.

The Solution to the error  #

Every Python programmer's best friend is Pip. The procedure of installing almost any Python package is simplified by the package manager by reducing it to a single command.

However, Pip isn't impervious to sporadic errors and problems that could occasionally impede your workflow. Let's dive into the solution of the ImportError: cannot import name ‘markup’ from ‘jinja2’

Solution one (Usual Import statement is not working)  #

First, I will explain the easy solution which is a syntax error or the usual import statement is not working and the user encounters the import error then use the below import statement but before this, it should be confirmed that the user has installed the jinja2 module and the flask module is installed properly and working.

from jinja2.utils import markupsafe markupsafe.Markup() Markup('')

This ought to fix the import error and get the package up and running well.

Solution two (Installation of Jinja2 and flask compatible versions)  #

Having the correct versions of Jinja2 and Flask is another simple fix for the issue. For both packages to function properly, you need the aforementioned versions or higher. The flask and jinja2 versions that work together are 2.1.0 and 3.1.1, respectively. Pip makes it rather simple to install both of them. You only need to type in each of the following commands individually to get started.

Information: It is the responsibility of the package installer to make sure that optional dependencies, including transitive dependencies, are appropriately installed.

You must make sure that the Flask version you are using is 2.1.0 or higher and that the Jinja2 version you are using is 3.1.1. These commands can be used to check the Jinja2 and Flask versions.

pip show jinja2

pip show Flask

Once you have checked the version and found that you are not using appropriate versions so install the appropriate versions that are compatible with each other. Run the below commands to install the compatible versions.

pip install Flask==2.1.0

pip install Jinja2==3.1.1

Try importing Markup from Jinja2 again after the installations are finished, and it should go well.

Solution three (Updation of jinja2 and flask)  #

It's also a good idea to ensure that all necessary packages are up to date if you're developing a Flask-based app. The following Pip command can be used to accomplish this.

pip install --upgrade [package name]

Updating the specific packages also solves the issue if the versions are not compatible with each other.

The Conclusion (Learning outcomes)  #

The article explains the ImportError: cannot import name ‘markup’ from ‘jinja2’, the error is self-explanatory. Error message explains itself as the module which is requested to import not being imported. The reasons are different, some time usual import statement did not work, and compatibility of the flask version and jinja2 module also can cause the error, due to unmatched versions Python may not be able to locate the package also another cause of the error. There are solutions available according to the prescribed scenarios, changing the usual import statement, updating the versions that are appropriate to each other, and installation of the compatible versions is also a solution to the error.  

Reference:  #

The Flask App

Exception Errors

Solved Errors

Study Expert

IT Source Code

Import Error

If you read this far, tweet to the author to show them you care. Tweet a Thanks

More Posts

Importerror: cannot import name 'url' from 'django.conf.urls' NoirHusky - Sep 22
Importerror: attempted relative import with no known parent package Ankur Ranpariya - Sep 26
Importerror: DLL load failed: Error in Python Ankur Ranpariya - Sep 6
Numpy.core.multiarray failed to import windows amr12345a - Jun 10
Importerror: no module named image amr12345a - Jun 10
Jsondecodeerror: expecting property name enclosed in double quotes. sadmin - May 11
Expecting property name enclosed in double quotes- Fixed sadmin - May 11
Cannot Unpack Non-Iterable Int Object zhteja - Sep 23
FileNotFoundError: [WinError 2] The system cannot find the file specified Ferdy - Sep 16
Valueerror: cannot convert non-finite values (na or inf) to integer Ankur Ranpariya 1 - Aug 22
Cannot perform reduce with flexible type Hussain Zafar - Jun 7
Sum of odd numbers from 1 to 50 in Python Ferdy - Aug 10
Sum of even numbers from 1 to 100 in Python Ferdy - Aug 8
How are python dictionaries different from python lists? Ferdy - Jul 23
Remove Commas from String Python Ankur Ranpariya - Jul 17
Python program to print even numbers from the list Ankur Ranpariya - Jul 16
Valueerror: could not broadcast input array from shape Hussain Zafar - Jun 18
Remove file extension from filename using python amr12345a - Jun 3
Define the missing method. use "this" to distinguish the local member from the parameter name. James - Jun 1
TypeError: 'int' object is not iterable in Python Ferdy - Sep 30