[Solved] Python/Docker ImportError: cannot import name 'json' from itsdangerous
Problem
There is an import error we might face while working with flask and docker together. It says
ImportError: cannot import name 'json' from ‘itsdangerous’
This error occurs because of the version of ‘itsdangerous’ package. This generally happens when we use flask version 2.0.0 and docker version 5.0.0. We can fix this problem through multiple solutions. Here I am going to show you some.
Solution 1
We need to define the package dependency of ‘itsdangerous’ package in the requirements.txt file.
itsdangerous==2.0.1
Just copy and paste this into the requirements.txt file. Then you need to update the virtual environment by the following command
pip install -r requirements.txt
You can use this command also to update the virtual environment
pip install -r requirements.txt --upgrade
Then run this command to get started
docker-compose up --build
Now everything should work perfectly. With this method, we don't need to reinstall or upgrade flask. However, if the error remains you can use the second solution.
Solution 2
If the previous solution didn’t work you can upgrade your flask version to flask 2.0.1 or higher. You can run the following command to update Flask.
pip install Flask=2.0.1
Although this method is time-consuming it will surely do the job.
Thank you for reading this article. Hope this solved your problem. If you face any problems further please comment below.