How To Fix Cannot import name ‘soft_unicode’ from ‘markupsafe’

You try to import soft_unicode from markupsafe, but Python fails to run this command. Also, you get the error message: ImportError: Cannot import name 'soft_unicode' from 'markupsafe'. In this article, we will go together to determine the cause and get solutions for this problem!

Cause of ImportError: Cannot import name ‘soft_unicode’ from ‘markupsafe’

The error message: ImportError: Cannot import name 'soft_unicode' from 'markupsafe' shows up when you import soft_unicode from markupsafe.

Here is the code sample that leads to the problem:

from markupsafe import soft_unicode

Your Python program stops running and delivers a message:

    from markupsafe import soft_unicode
ImportError: Cannot import name 'soft_unicode' from 'markupsafe'

There is nothing wrong with your command. The ImportError occurs due to the incompatibility of the markupsafe package version.

On the markupsafe website, there is an announcement that the soft_unicode, which was previously deprecated, has been ejected from the markupsafe package – version 2.1.x (released from 17 February 2022).

Solutions for ImportError: Cannot import name ‘soft_unicode’ from ‘markupsafe’

Here are 2 solutions for ImportError: Cannot import name 'soft_unicode' from 'markupsafe':

Downgrade the markupsafe version

There is no way to use soft_unicode from the markupsafe package version 2.1.x and above. You can only downgrade the version of this package to take advantage of soft_unicode.

Here is how you downgrade the version:

Step 1: Open the Command Prompt

You open the Command Prompt by searching for it in the search bar on your computer. Or simply click on Windows + R, input cmd, and click on OK. The Command Prompt will show up.

Step 2: Downgrade the markupsafe package  to the version 2.0.0

You rely on the pip package to install a package to your Python. 

The syntax is like this:

python -m pip install markupsafe==[version]

For the [version], you should input 2.0.0. In this version, soft_unicode is still available to use.

Step 3: Try running the program using soft_unicode

You complete downgrading the version of the markupsafe package to 2.0.0. In this version, soft_unicode is still available for use.

You can test by writing a Python project. Don’t forget to import soft_unicode before taking advantage of its features.

Here is the syntax:

from markupsafe import soft_unicode
print(soft_unicode([parameter]))

You can add anything as the parameter for the soft_unicode(). It takes different data types, including integer, string, list, boolean, and so on!

Here is the code sample:

from markupsafe import soft_unicode

print(soft_unicode(58))
print(soft_unicode("Learn Share It"))
print(soft_unicode([324]))

The output will be:

58
Learn Share It
[324]

Use soft_str instead of soft_unicode

MarkupSafe has given a DeprecationWarning, which describes that the soft_unicode has been changed to soft_str. The old name is ejected from MarkupSafe 2.1.x.

As you see, the soft_unicode has been replaced by the soft_str from the markupsafe – version 2.1.x and above.

If you don’t want to downgrade your markupsafe, consider using soft_str instead of soft_unicode.

Here is the syntax:

from markupsafe import  soft_str
print(soft_str([parameter]))

You use the function the same as you use soft_unicode. The difference is just in the name.

Here is the code sample:

from markupsafe import soft_str

print(soft_str(58))
print(soft_str("Learn Share It"))
print(soft_str([324]))

The output will be:

58
Learn Share It
[324]

Summary

In conclusion, the ImportError: Cannot import name 'soft_unicode' from 'markupsafe' occurs when you are using soft_unicode in the markupsafe – version 2.1.x or above. The soft_unicode has been removed from the markupsafe 2.1. Therefore, you need to downgrade the version to use this feature or use the soft_str instead of soft_unicode.

Leave a Reply

Your email address will not be published. Required fields are marked *