Last updated: Apr 10, 2024
Reading timeยท3 min
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' 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
.
markupsafe
to version 2.0.1
The first thing you should try is to install version 2.0.1
of markupsafe
.
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
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.".
2.0.1
of the markupsafe
package is installed successfully.If you have a requirements.txt file, you can add the following line.
markupsafe==2.0.1
pip install
command with the --force-reinstall
optionYou can also try to run the pip install
command with the
--force-reinstall option when
installing version 2.0.1
.
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
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.
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.
- 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.
markupsafe
moduleIf the error is not resolved, try to uninstall markupsafe
and then install
version 2.0.1
.
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.
pip show markupsafe pip3 show markupsafe python -m pip show markupsafe python3 -m pip show markupsafe
If version 2.0.1
is installed, the error should be resolved.
markupsafe
package to version 2.0.1
in your config file.args: AIRFLOW_DEPS: gcp_api,s3 PYTHON_DEPS: MarkupSafe==2.0.1
If the error persists and you get it when using
Flask and Jinja2
, try to uninstall them
and install the latest version.
pip uninstall Flask Jinja2 pip uninstall Flask Jinja2 pip install Flask Jinja2 --upgrade pip3 install Flask Jinja2 --upgrade
When the soft_unicode
method was removed, it was replaced by soft_str
.
markupsafe.soft_str
method insteadIf you are calling the method in your own code, you can use the
markupsafe.soft_str()
method instead.
from markupsafe import escape, soft_str value = escape('<example>') print(escape(str(value))) # ๐๏ธ &lt;example&gt; print(escape(soft_str(value))) # ๐๏ธ <example>
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.
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