If you’ve encountered the “ImportError: cannot import name ‘json’ from ‘itsdangerous’” error, you’re not alone. This error occurs because the JSON module has been removed from its “itsdangerous” library starting with version 2.1.0. In this article, we’ll discuss how to solve this error and get your code running smoothly again.
Solution 1: Install Version 2.0.1 of the itsdangerous Module
One way to solve the error is to install version 2.0.1 of the “itsdangerous “module. This can be done using the following command:
pip install itsdangerous==2.0.1 --force-reinstall
If you’re using Python 3, use the following command instead:
pip3 install itsdangerous==2.0.1 --force-reinstall
If you don’t have pip in your PATH environment variable, you can use the following commands:
python -m pip install itsdangerous==2.0.1 --force-reinstall
python3 -m pip install itsdangerous==2.0.1 --force-reinstall
On Windows, you can use the py
alias:
py -m pip install itsdangerous==2.0.1 --force-reinstall
If you’re using Jupyter Notebook, you can use the following command:
!pip install itsdangerous==2.0.1 --force-reinstall
Don’t forget to update your requirements.txt
file with the following line:
itsdangerous==2.0.1
You might encounter an error that states “ERROR: pip’s dependency resolver does not currently take into account all the packages that are installed.” However, even though the error is shown, the package is usually installed successfully.
[Fixed]: Solved: createRoot(): Target container is not a DOM element
Solution 2: Use the Standard Library Instead of itsdangerous
If you have an import in your code that looks like this:
# old import
from itsdangerous import json
Replace it with an import from the standard library instead:
# correct import
import json
Solution 3: Upgrade Flask
If you’re using the Flask module, you can try upgrading your version of Flask using the following command:
pip install Flask --upgrade
For Python 3, use the following command instead:
pip3 install Flask --upgrade
If you don’t have pip in your PATH environment variable, you can use the following commands:
python -m pip install Flask --upgrade
python3 -m pip install Flask --upgrade
On Windows, you can use the py
alias:
py -m pip install Flask --upgrade
If you’re using Jupyter Notebook, you can use the following command:
!pip install Flask --upgrade
Solution 4: Upgrade All Outdated Packages
If none of the above solutions have worked for you, you can try upgrading all outdated packages in your environment. The most straightforward way to do this is to use the following Python script:
import pkg_resources
from subprocess import call
packages = [dist.project_name for dist in pkg_resources.working_set]
call("pip install --upgrade " + ' '.join(packages), shell=True)
Save the script in a file (e.g. main.py) and run it using python main.py to upgrade all outdated packages.
Alternatively, you can use the following command on macOS or Linux:
pip install -U `pip list --outdated | awk 'NR>2 {print $1}'`
On Windows, you can use the following command:
for /F "delims= " %i in ('pip list --outdated') do pip install -U %i
If you’re using a requirements.txt file, you can update it with the following command:
pip freeze > requirements.txt
Conclusion on ImportError: cannot import name ‘json’ from ‘itsdangerous’
The “ImportError: cannot import name ‘json’ from ‘itsdangerous’” error is often caused by an outdated package that tries to import JSON from the “itsdangerous” module. To solve the error, you can try installing an earlier version of the itsdangerous module, using the standard library instead of itsdangerous, upgrading Flask, or upgrading all outdated packages in your environment.
I hope this article has helped you solve the ImportError: cannot import name ‘json’ from ‘itsdangerous’ error. If you have any questions or suggestions, please leave a comment below.