ImportError: cannot import name 'json' from itsdangerous

Question:

I am trying to get a Flask and Docker application to work but when I try and run it using my docker-compose up command in my Visual Studio terminal, it gives me an ImportError called ImportError: cannot import name 'json' from itsdangerous. I have tried to look for possible solutions to this problem but as of right now there are not many on here or anywhere else. The only two solutions I could find are to change the current installation of MarkupSafe and itsdangerous to a higher version: https://serverfault.com/questions/1094062/from-itsdangerous-import-json-as-json-importerror-cannot-import-name-json-fr and another one on GitHub that tells me to essentially change the MarkUpSafe and itsdangerous installation again https://github.com/aws/aws-sam-cli/issues/3661, I have also tried to make a virtual environment named veganetworkscriptenv to install the packages but that has also failed as well. I am currently using Flask 2.0.0 and Docker 5.0.0 and the error occurs on line eight in vegamain.py.

Here is the full ImportError that I get when I try and run the program:

veganetworkscript-backend-1  | Traceback (most recent call last):
veganetworkscript-backend-1  |   File "/app/vegamain.py", line 8, in <module>
veganetworkscript-backend-1  |     from flask import Flask
veganetworkscript-backend-1  |   File "/usr/local/lib/python3.9/site-packages/flask/__init__.py", line 19, in <module>
veganetworkscript-backend-1  |     from . import json
veganetworkscript-backend-1  |   File "/usr/local/lib/python3.9/site-packages/flask/json/__init__.py", line 15, in <module>
veganetworkscript-backend-1  |     from itsdangerous import json as _json
veganetworkscript-backend-1  | ImportError: cannot import name 'json' from 'itsdangerous' (/usr/local/lib/python3.9/site-packages/itsdangerous/__init__.py)
veganetworkscript-backend-1 exited with code 1

Here are my requirements.txt, vegamain.py, Dockerfile, and docker-compose.yml files:

requirements.txt:

Flask==2.0.0
Flask-SQLAlchemy==2.4.4
SQLAlchemy==1.3.20
Flask-Migrate==2.5.3
Flask-Script==2.0.6
Flask-Cors==3.0.9
requests==2.25.0
mysqlclient==2.0.1
pika==1.1.0
wolframalpha==4.3.0

vegamain.py:

# Veganetwork (C) TetraSystemSolutions 2022
# all rights are reserved.  
# 
# Author: Trevor R. Blanchard Feb-19-2022-Jul-30-2022
#

# get our imports in order first
from flask import Flask # <-- error occurs here!!!

# start the application through flask.
app = Flask(__name__)

# if set to true will return only a "Hello World" string.
Debug = True

# start a route to the index part of the app in flask.
@app.route('/')
def index():
    if (Debug == True):
        return 'Hello World!'
    else:
        pass

# start the flask app here --->
if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0') 

Dockerfile:

FROM python:3.9
ENV PYTHONUNBUFFERED 1
WORKDIR /app
COPY requirements.txt /app/requirements.txt
RUN pip install -r requirements.txt
COPY . /app

docker-compose.yml:

version: '3.8'
services:
  backend:
    build:
      context: .
      dockerfile: Dockerfile
    command: 'python vegamain.py'
    ports:
      - 8004:5000
    volumes:
      - .:/app
    depends_on:
      - db

#  queue:
#    build:
#      context: .
#      dockerfile: Dockerfile
#    command: 'python -u consumer.py'
#    depends_on:
#      - db

  db:
    image: mysql:5.7.22
    restart: always
    environment:
      MYSQL_DATABASE: admin
      MYSQL_USER: root
      MYSQL_PASSWORD: root
      MYSQL_ROOT_PASSWORD: root
    volumes:
      - .dbdata:/var/lib/mysql
    ports:
      - 33069:3306

How exactly can I fix this code? thank you!

Asked By: KronosHedronos2077

||

Answers:

I was facing the same issue while running docker containers with flask.

I downgraded Flask to 1.1.4 and markupsafe to 2.0.1 which solved my issue.

Check this for reference.

Answered By: Harshit Saini

The import failure seems to be related with the latest release version of the package itsdangerous.
Check the latest releases here

In my case, I made it work by explicitly adding the package dependency itsdangerous==2.0.1 in my requirements.txt file.

To make changes effective, update your virtual environment to reflect the new requirements.txt.

Answered By: JoeBigToe

The root of this issue appears to be that Flask 2.0.0 is unsupported. Try using the latest version of Flask.

https://github.com/pallets/itsdangerous/issues/289

As of March 8 2022, the latest version is 2.0.3:

pip install Flask=2.0.3

See Flask source code on Github for the most current version.

Answered By: Christopher Diep

Adding itsdangerous==2.0.1 to my requirements.txt file, and downgrading to Flask==1.1.1 fixed it for me.

Answered By: joe hoeller

Solution 1 – Upgrade the Flask to latest version i.e, 2.0.1 or above. It’s the recommended way to fix the issue.

pip install Flask=2.0.1

OR

pip install Flask=2.1.0

Solution 2 – Since it difficult to upgrade to latest version of Flask in shorter time, you can try below methods to resolve the issue.

you can continue using the Flask version 1.1.2 and try downgrading the itdangerous to 2.0.1. You can do this by adding itsdangerous==2.0.1 to your requirements.txt file.

Solution 3 – Another way is to upgrade Flask from 1.1.2 to 1.1.4 as it does not have ground breaking changes and also downgrade the markupsafe library to 2.0.1

Once you upgrade from Flask 1.1.2 to 1.1.4 you will face another issue after, which is ImportError: cannot import name ‘soft_unicode’ from ‘markupsafe’ in Release 1.38.0 #3661, and that can be fixed by downgrading the markupsafe to version 2.0.1 as shown below.

pip install Flask==1.1.4
pip install markupsafe==2.0.1

Reference – [Solved] ImportError: cannot import name ‘json’ from itsdangerous

I just put itsdangerous==2.0.1 in my requirements.txt .Then updated my virtualenv using pip install -r requirements.txt and then docker-compose up --build . Now everything fine for me. Didnot upgrade the flask version.

Answered By: Safayet Jamil

Running pip install itsdangerous==2.0.1 in my virtual environment worked for me. Note: I’m using on my Windows 10 machine

Answered By: stealth225

This thread was useful for me too.

markup safe didn’t quite swing it.

Itisdangerous=2.0.1 was the fifth on the list of my requirements, restored the passenger file, and smooth sailing from there!

Answered By: Omegaman

I uninstalled flask and reinstalled it:

  • conda uninstall -c anaconda flask
  • conda install -c anaconda flask

The following packages were installed: dataclasses-0.8, flask-2.0.3, itsdangerous-2.0.1 , werkzeug-2.0.3

Answered By: Carl

Downgrade the ItstooDangerous to a lower version to support the flask.

pip3 install itsdangerous==2.0.1
Answered By: Poornachandra Kashi

enter image description here

yep it seems itsdangerous decided to ‘Import json from the standard library instead’ from version 2.1.0 onwards https://itsdangerous.palletsprojects.com/en/2.1.x/changes/

So the following should resolve this particular error:
pip install itsdangerous==2.0.1

Answered By: al-baba
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.