Resolving “ImportError: cannot import name ‘json’ from ‘itsdangerous'” in Flask GCP Deployment


3 views

This error typically occurs when there's a version mismatch between Flask and itsdangerous. The Flask framework's JSON module tries to import a JSON utility from itsdangerous that was removed in newer versions of the package.

The breaking change happened when itsdangerous 2.1.0 removed the json module that Flask was trying to import. Here's how to check your current versions:

pip show flask itsdangerous
# Expected output before the breaking change:
# Flask==2.0.3
# itsdangerous==2.0.1

You have two primary solutions:

# Option 1: Pin to compatible versions
pip install flask==2.0.3 itsdangerous==2.0.1

# Option 2: Upgrade to newer Flask version that handles this properly
pip install --upgrade flask

For Google Cloud Platform deployments, you'll want to modify your requirements.txt:

# requirements.txt
flask>=2.1.0  # Version that handles the itsdangerous change
# OR if you need specific older versions
flask==2.0.3
itsdangerous==2.0.1

After implementing the fix, test locally before redeploying:

python -c "from flask import Flask; print('Import successful')"

If you're using other Flask extensions that might cause similar conflicts, consider:

# Using a virtual environment
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt

For GCP App Engine, check your deployment logs:

gcloud app logs tail -s default

And verify installed versions in your GCP environment:

# Add this to a temporary route in your Flask app
@app.route('/versions')
def show_versions():
    import pkg_resources
    return {
        'flask': pkg_resources.get_distribution('flask').version,
        'itsdangerous': pkg_resources.get_distribution('itsdangerous').version
    }

When deploying Flask applications to Google Cloud Platform, many developers encounter this particularly frustrating error related to the itsdangerous package. The core issue stems from version incompatibilities between Flask and itsdangerous, where newer versions of itsdangerous (2.1.0+) removed the json module that Flask expects to import.

The error occurs because:

flask.json.__init__.py tries to import:
from itsdangerous import json as _json

But in itsdangerous>=2.1.0, this module no longer exists. There are three main approaches to resolve this:

The most reliable fix is to specify compatible versions in your requirements.txt:

Flask==2.0.3
itsdangerous==2.0.1

Or using pip:

pip install Flask==2.0.3 itsdangerous==2.0.1

If you can use newer Flask versions (2.1.0+), they've fixed this dependency:

Flask>=2.1.0
itsdangerous>=2.0.0

For GCP App Engine specifically, add this to your app.yaml:

env_variables:
  PIP_NO_DEPS: "True"
runtime: python39
entrypoint: gunicorn -b :$PORT main:app

And create a requirements.txt with exact versions.

After applying any solution, test locally before deploying:

python -c "from itsdangerous import json; print('Import works')"

Or for newer Flask versions:

python -c "from flask import Flask; print('Flask imports correctly')"

Consider these best practices:

  • Always pin major dependency versions
  • Test deployment environments locally using Docker
  • Implement CI/CD pipeline with version checks

For more control, use virtual environments with exact package versions:

python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
gcloud app deploy