Cannot import name 'soft_unicode' from 'markupsafe' [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
3 min

banner

# Cannot import name 'soft_unicode' from 'markupsafe' [Solved]

The "ImportError: cannot import name 'soft_unicode' from 'markupsafe'" occurs because the soft_unicode method has been deprecated in markupsafe version 2.1.0.

To solve the error, run the pip install markupsafe==2.0.1 command to install the last version of markupsafe that supports soft_unicode.

importerror cannot import name soft unicode

shell
ImportError: cannot import name 'soft_unicode' from 'markupsafe' (/home/borislav/Desktop/bobbyhadz_python/venv/lib/python3.11/site-packages/markupsafe/__init__.py)

The changes section of the markupsafe documentation shows that the soft_unicode method has been deprecated and removed starting with version 2.1.0.

soft unicode has been deprecated

# Pin markupsafe to version 2.0.1

The first thing you should try is to install version 2.0.1 of markupsafe.

shell
pip install markupsafe==2.0.1 pip3 install markupsafe==2.0.1 # ๐Ÿ‘‡๏ธ If you don't have pip in PATH environment variable python -m pip install markupsafe==2.0.1 python3 -m pip install markupsafe==2.0.1 # ๐Ÿ‘‡๏ธ py alias (Windows) py -m pip install markupsafe==2.0.1 # ๐Ÿ‘‡๏ธ For Jupyter Notebook !pip install markupsafe==2.0.1

install markupsafe 2 0 1

When you run the command, you might get an error that states "ERROR: pip's dependency resolver does not currently take into account all the packages that are installed.".

However, even though the error is shown, version 2.0.1 of the markupsafe package is installed successfully.

If you have a requirements.txt file, you can add the following line.

requirements.txt
markupsafe==2.0.1

# Running the pip install command with the --force-reinstall option

You can also try to run the pip install command with the --force-reinstall option when installing version 2.0.1.

shell
pip install markupsafe==2.0.1 --force-reinstall pip3 install markupsafe==2.0.1 --force-reinstall # ๐Ÿ‘‡๏ธ If you don't have pip in PATH environment variable python -m pip install markupsafe==2.0.1 --force-reinstall python3 -m pip install markupsafe==2.0.1 --force-reinstall py -m pip install markupsafe==2.0.1 --force-reinstall # ๐Ÿ‘‡๏ธ For Jupyter Notebook !pip install markupsafe==2.0.1 --force-reinstall

force reinstall markupsafe

# Solve the error when using AWS SAM CLI

If you got the error when using the AWS SAM CLI, you have to upgrade your SAM CLI version as there was an issue that got fixed in SAM CLI v1.38.

main.py
pip install --user --upgrade aws-sam-cli pip3 install --user --upgrade aws-sam-cli python -m pip install --user --upgrade aws-sam-cli python3 -m pip install --user --upgrade aws-sam-cli # ๐Ÿ‘‡๏ธ py alias (Windows) py -m pip install --user --upgrade aws-sam-cli

Note that it is recommended to install the AWS SAM CLI using the official operating systems-specific installers from the docs.

If you use GitHub actions, you can specify the version explicitly.

workflow.yml
- uses: aws-actions/setup-sam@v1 with: version: 1.37.0

Using pip is not the recommended way to install the SAM CLI as it often causes issues.

# Reinstall the markupsafe module

If the error is not resolved, try to uninstall markupsafe and then install version 2.0.1.

shell
pip uninstall markupsafe pip3 uninstall markupsafe pip install markupsafe==2.0.1 pip3 install markupsafe==2.0.1

You can use the pip show markupsafe command to check which version of the package is installed.

shell
pip show markupsafe pip3 show markupsafe python -m pip show markupsafe python3 -m pip show markupsafe

verify markupsafe version

If version 2.0.1 is installed, the error should be resolved.

If you use Docker or Github actions, you have to pin the markupsafe package to version 2.0.1 in your config file.
Dockerfile
args: AIRFLOW_DEPS: gcp_api,s3 PYTHON_DEPS: MarkupSafe==2.0.1

# Reinstall Flask and Jinja2 if you use them

If the error persists and you get it when using Flask and Jinja2, try to uninstall them and install the latest version.

shell
pip uninstall Flask Jinja2 pip uninstall Flask Jinja2 pip install Flask Jinja2 --upgrade pip3 install Flask Jinja2 --upgrade

upgrade flask and jinja

When the soft_unicode method was removed, it was replaced by soft_str.

# Using the markupsafe.soft_str method instead

If you are calling the method in your own code, you can use the markupsafe.soft_str() method instead.

main.py
from markupsafe import escape, soft_str value = escape('<example>') print(escape(str(value))) # ๐Ÿ‘‰๏ธ &amp;lt;example&amp;gt; print(escape(soft_str(value))) # ๐Ÿ‘‰๏ธ &lt;example&gt;

use soft str method instead

The markupsafe.soft_str() method converts an object to a string, preserving a Markup string, rather than converting it back to a basic string.

The string is marked as safe and isn't escaped again.

If you'd rather use the soft_str method instead of soft_unicode, you can upgrade the markupsafe package to the latest version.

shell
pip install markupsafe --upgrade pip3 install markupsafe --upgrade # ๐Ÿ‘‡๏ธ If you don't have pip in PATH environment variable python -m pip install markupsafe --upgrade python3 -m pip install markupsafe --upgrade # ๐Ÿ‘‡๏ธ py alias (Windows) py -m pip install markupsafe --upgrade # ๐Ÿ‘‡๏ธ For Jupyter Notebook !pip install markupsafe --upgrade
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev