Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

ImportError with Flask Application on Google Cloud: Cannot import name 'json' from 'itsdangerous'

Hello everyone,

I am encountering an issue while deploying a Flask-based chatbot application to Google Cloud. The application runs perfectly fine locally but throws an ImportError when deployed to Google Cloud.

Environment:

  • Runtime: Python 3.11 (as specified in app.yaml)
  • Framework: Flask 2.3.3
  • Deploying to: Google Cloud

Error Log: The error I am encountering is as follows:

2023-08-22 01:53:48 default[20230822t035216] "GET /favicon.ico HTTP/1.1" 500
2023-08-22 01:53:48 default[20230822t035216] Traceback (most recent call last): File "main.py", line 2, in <module> from flask import Flask, request, jsonify, render_template File "/layers/google.python.pip/pip/lib/python3.7/site-packages/flask/__init__.py", line 19, in <module> from . import json File "/layers/google.python.pip/pip/lib/python3.7/site-packages/flask/json/__init__.py", line 15, in <module> from itsdangerous import json as _json ImportError: cannot import name 'json' from 'itsdangerous' (/layers/google.python.pip/pip/lib/python3.7/site-packages/itsdangerous/__init__.py)

Files:

  1. main.py: Contains the main code for the chatbot.

  2. index.html: HTML file for rendering the interface.

  3. app.yaml:

    runtime: python311
    entrypoint: python main.py

    env_variables:
    OPENAI_API_KEY: 'sk-CODE'

    requirements.txt:

    aiofiles==23.2.1
    aiohttp==3.8.5
    aiosignal==1.3.1
    altair==5.0.1
    annotated-types==0.5.0
    anyio==3.7.1
    async-timeout==4.0.3
    attrs==23.1.0
    blinker==1.6.2
    cachelib==0.10.2
    certifi==2023.7.22
    charset-normalizer==3.2.0
    click==8.1.6
    colorama==0.4.6
    contourpy==1.1.0
    cycler==0.11.0
    et-xmlfile==1.1.0
    fastapi==0.101.1
    ffmpy==0.3.1
    filelock==3.12.2
    Flask==2.3.3
    Flask-Cors==4.0.0
    Flask-Session==0.5.0
    fonttools==4.42.0
    frozenlist==1.4.0
    fsspec==2023.6.0
    gradio==3.40.1
    gradio_client==0.4.0
    gunicorn==21.2.0
    h11==0.14.0
    httpcore==0.17.3
    httpx==0.24.1
    huggingface-hub==0.16.4
    idna==3.4
    importlib-resources==6.0.1
    itsdangerous==2.1.2
    Jinja2==3.1.2
    jsonschema==4.19.0
    jsonschema-specifications==2023.7.1
    kiwisolver==1.4.4
    linkify-it-py==2.0.2
    markdown-it-py==2.2.0
    MarkupSafe==2.1.3
    matplotlib==3.7.2
    mdit-py-plugins==0.3.3
    mdurl==0.1.2
    multidict==6.0.4
    numpy==1.25.2
    openai==0.27.0
    openpyxl==3.1.2
    orjson==3.9.5
    packaging==23.1
    pandas==2.0.3
    Pillow==10.0.0
    pydantic==2.1.1
    pydantic_core==2.4.0
    pydub==0.25.1
    pyparsing==3.0.9
    python-dateutil==2.8.2
    python-multipart==0.0.6
    pytz==2023.3
    PyYAML==6.0.1
    referencing==0.30.2
    requests==2.31.0
    rpds-py==0.9.2
    semantic-version==2.10.0
    six==1.16.0
    sniffio==1.3.0
    starlette==0.27.0
    toolz==0.12.0
    tqdm==4.66.1
    typing_extensions==4.7.1
    tzdata==2023.3
    uc-micro-py==1.0.2
    urllib3==2.0.4
    uvicorn==0.23.2
    websockets==11.0.3
    Werkzeug==2.3.7
    yarl==1.9.2

Attempts to Resolve:

  • Checked Python version consistency.
  • Updated itsdangerous package.
  • Updated Flask.
  • Verified dependencies for compatibility.
  • Tested locally using the same Python version as in app.yaml.
  • Followed Google Cloud documentation for deploying Python applications.
  • Rebuilt the virtual environment in Google Cloud with updated dependencies.

Unfortunately, none of the above steps resolved the issue. It seems that neither the dependency nor Python get updated.

Request for Help: I would greatly appreciate any insights or suggestions on how to resolve this ImportError. If anyone has encountered a similar issue or knows what might be causing this problem, please share your thoughts. I´m very new to programming in general so I apologize in advance if this is some very obvious error.

Thank you in advance for your assistance!

Best regards, Noah



Solved Solved
0 2 6,851
1 ACCEPTED SOLUTION

When you say it works locally, did you run it using dev_appserver.py? If you didn't, then you've bypassed  app.yaml

Remove 'entrypoint: python main.py' or replace it with entrypoint: gunicorn -b :$PORT main:app and see if that solves your problem

..... NoCommandLine ......
 https://nocommandline.com
A GUI for Google App Engine

View solution in original post

2 REPLIES 2

When you say it works locally, did you run it using dev_appserver.py? If you didn't, then you've bypassed  app.yaml

Remove 'entrypoint: python main.py' or replace it with entrypoint: gunicorn -b :$PORT main:app and see if that solves your problem

..... NoCommandLine ......
 https://nocommandline.com
A GUI for Google App Engine

I have no idea why, but this works perfectly. Thank you so much!