How to fix ImportError: cannot import name 'soft_unicode' from 'markupsafe'

One error that you might encounter when running Python code is:

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

This error occurs because the soft_unicode module has been removed and replaced with soft_str module in MarkupSafe version 2.1.0

The solution to fix this error depends on whether you use the module directly in your code or you have a dependency on MarkupSafe.

1. Error caused by using MarkupSafe in your source code

If you use the soft_unicode function in your code, you need to replace the function with soft_str to resolve this error:

# From this:
from markupsafe import soft_unicode

res = soft_unicode('<script>')

# Change to this:
from markupsafe import soft_str

res = soft_str('<script>')

After you replace all occurrences of soft_unicode with soft_str, the error should be resolved.

2. Error caused by dependency packages

This error can also be caused by a package that depends on markupsafe like jinja2 and flask, then you need to upgrade those packages to their latest versions.

Use pip to upgrade these packages:

pip install flask jinja2 --upgrade

# For pip3:
pip3 install flask jinja2 --upgrade

With that, you should have the latest versions of jinja2 and flask, which resolves the error.

Another package that has been known to cause this error is aws-sam-cli, which uses an outdated version of jinja2. The fix was released in SAM CLI v1.38.1, so you need to upgrade the package:

pip install aws-sam-cli --upgrade

pip3 install aws-sam-cli --upgrade

Please note that installing aws-sam-cli using pip frequently causes an error. You should install the package using the provided installer.

Once you upgrade the CLI, you won’t receive this error.

3. Downgrading MarkupSafe

One often suggested solution for this error is to downgrade the markupsafe package to the version that still has the soft_unicode module.

However, I don’t recommend doing this because you usually have other dependencies that require the latest version of markupsafe.

But if you absolutely can’t upgrade the dependencies or modify the source code, then you can run this command:

pip install markupsafe==2.0.1 --force-reinstall
# or
pip3 install markupsafe==2.0.1 --force-reinstall

The command should install markupsafe version 2.0.1, and you can import the soft_unicode module just fine.

Conclusion

The ImportError: cannot import name 'soft_unicode' from 'markupsafe' occurs when you use the soft_unicode function in your Python project.

The function has been removed in the latest version of markupsafe, so you need to modify the source code to use soft_str in its place.

Packages that depend on markupsafe should’ve released a new version that resolves this issue, so you need to upgrade them.

If you need to maintain compatibility with soft_unicode, then you can downgrade markupsafe to version 2.0.1.

I hope this tutorial helps. See you in other tutorials! 🙌

Take your skills to the next level ⚡️

I'm sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.