If you are using the Docker container and Flask version 1.1.2, you might be facing the Error “ImportError: cannot import name ‘json’ from itsdangerous”.
In this article, we will check how we can fix the error “ImportError: cannot import name ‘json’ from itsdangerous” step by step and with all possible ways.
What is “ImportError: cannot import name ‘json’ from itsdangerous” Error?
“ImportError: cannot import name ‘json’ from itsdangerous” error is mainly seen when you are using Python application and running application with flask version 1.1.2 or Flask version 1.1.4.
When we are using the Flask application, it has dependency mainly on two packages:
- MarkupSafe: MarkupSafe is used to implement the text object to replace the special meaning characters in XML and HTML, this way, MarkupSafe helps to mitigate the injection attacks.
- ItsDangerous: Using ItsDangerous data is signed securely using cryptographic methods. Data is compressed and timestamps are used to validate token’s authenticity and integrity.
Why “ImportError: cannot import name ‘json’ from itsdangerous” Error Occurred?
Initially, Flask version is updated from 1.1.2 to Flask version 1.1.4, still error ” https://github.com/aws/aws-sam-cli/issues/3661 ” is seen. So issue seems related to the ” https://github.com/aws/aws-sam-cli/issues/3661 “.
So Mainly Error “ImportError: cannot import name ‘json’ from itsdangerous” is because of upgrade in MarkupSafe:2.1.0 in which soft_unicode has been removed.
How to Fix “ImportError: cannot import name ‘json’ from itsdangerous” Error?
There are multiple methods to fix the error “ImportError: cannot import name ‘json’ from itsdangerous“, below are the the all ways to fix the error “ImportError: cannot import name ‘json’ from itsdangerous“:
Solution 1: Upgrade Flask Version
Best and easy way to resolve the issue to upgrade the Flask to it’s latest version, latest version at time of writing post is “Flask 2.1.2” and available on this link.
But if you are using very older Flask version and because of any reason, want to stick with that, then below mentioned solution can work for you:
Solution 2: Downgrade MarkupSafe to 2.0.1
As an alternate solution, you can upgrade the Flask version to 1.1.4 or above, which bring the another issue “ImportError: cannot import name ‘soft_unicode’ from ‘markupsafe’ in Release 1.38.0 #3661“. So to fix the consecutive issue, downgrade the MarkupSafe version to 2.0.1 version.
Below the commands those you can use to Upgrade Flask version to 2.1.2 and downgrade the MarkupSafe version to 2.0.1:
pip install Flask==1.1.4
pip install markupsafe==2.0.1
Solution 3: Downgrade itsdangerous version to 2.0.1
Last and foremost way is to downgrade the Downgrade the itsdangerous version to 2.0.1. Below command will help to downgrade the version of itsdangerous package to 2.0.1:
pip install itsdangerous==2.0.1
Conclusion
If you are using the Flask 1.1.2 version application using Docker containers, you will see the below error: “cannot import name ‘json’ from itsdangerous Python/Docker”. As we discussed, there are three above mentioned proven methods to fix the “cannot import name ‘json’ from itsdangerous Python/Docker” error.
If you are using Flask version 1.1.2, issue can be fixed by downgrading the itsdangerous version to 2.0.1 OR if using the Flask version 1.1.4 and facing consicutive error “soft_unicode” then downgrade the MarkupSafe to version 2.0.1.
I hope above article helped you to fix the “cannot import name ‘json’ from itsdangerous Python/Docker” error.