Table of Contents Hide
- What is ImportError: cannot import name ‘json’ from itsdangerous?
- How to fix ImportError: cannot import name ‘json’ from itsdangerous
If you are deploying and running the flask application(1.1.2) using the Docker containers, you will get ImportError: cannot import name ‘json’ from itsdangerous
In this article, we will look at what is ImportError: cannot import name ‘json’ from itsdangerous means and how to fix the issue.
What is ImportError: cannot import name ‘json’ from itsdangerous?
The issue is faced mainly when you use the Python application and running the application with Flask version 1.1.2 or Flask version 1.1.4.
The Flask application has a dependency on the below two packages.
- MarkupSafe comes with Jinja. It escapes untrusted input when rendering templates to avoid injection attacks.
- ItsDangerous securely signs data to ensure its integrity. This is used to protect Flask’s session cookie.
Even if you upgrade the Flask version to 1.1.4 you will still get ‘soft_unicode error.
This issue seems to be related: ImportError: cannot import name ‘soft_unicode’ from ‘markupsafe’ in Release 1.38.0 #3661
It looks the issue is due to an upgrade in MarkupSafe:2.1.0 where they have removed soft_unicode. Checkout the Release Notes for more details.
It is also a breaking change in markupsafe and jinja not specifying an upper version bound pallets/markupsafe#286
How to fix ImportError: cannot import name ‘json’ from itsdangerous
Solution 1 – Upgrade the Flask to latest version > 2
The best way to resolve this issue is to upgrade the Flask to the latest version, i.e, 2.0.1 or above.
This will be a major upgrade if you are using the older Flask version like 1.1.2 and may have to test the entire application.
As an immediate solution If you still want to stick with the same Flask version and resolve this error, then you can go through the below solutions.
Solution 2 – Upgrade Flask to 1.1.4 and downgrade the markupsafe to 2.0.1
The issue can be fixed by upgrading the Flask version to 1.1.4 or above. However, you will face another issue after upgrading to 1.1.4, which is ImportError: cannot import name ‘soft_unicode’ from ‘markupsafe’, and that can be fixed by downgrading the markupsafe to version 2.0.1 as shown below.
- Upgrade the Flask version from 1.1.2 to 1.1.4
- Downgrade the markupsafe to 2.0.1
Commands to upgrade the Flask version and downgrade the markupsafe library.
pip install Flask==1.1.4 pip install markupsafe==2.0.1
Solution 3 – Downgrade itsdangerous to 2.0.1
Flask 1.1.2 is set up to require itsdangerous >= 0.24. The latest released (itsdangerous) version (2.10) deprecated the JSON API.
If you would want to continue using Flask 1.1.2, you need to require at most itsdangerous 2.0.1 (not 2.10). You can do this by adding itsdangerous==2.0.1 to your requirements.txt file. This will downgrade the version of itsdangerous package
pip install itsdangerous==2.0.1
Refer itsdangerous 2.10 changelog for more details.
If you are deploying and running the flask application with 1.1.2 version by using the Docker containers, you will get ImportError: cannot import name ‘json’ from itsdangerous
The Flask version 1.1.2 has a dependency on markupsafe and itsdangerous packages. The issue is with MarkupSafe:2.1.0 where they have removed soft_unicode.
If you are using Flask version 1.1.2, you can fix the issue by downgrading the itsdangerous version to 2.0.1
If you have upgraded to Flask version 1.1.4 and facing a soft_unicode error then you can downgrade the markupsafe to version 2.0.1