20

On deploying a Flask application to Google Cloud Platform, I get this error:

[ERROR] Exception in worker process from itsdangerous import json as _json ImportError: cannot import name 'json' from 'itsdangerous'

2022-02-18 08:00:30 default[20220218t132659]  Traceback (most recent call last):
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker
  worker.init_process()
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/gunicorn/workers/gthread.py", line 92, in init_process
  super().init_process()
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/gunicorn/workers/base.py", line 134, in init_process
  self.load_wsgi()
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
  self.wsgi = self.app.wsgi()
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
  self.callable = self.load()
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
  return self.load_wsgiapp()
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
  return util.import_app(self.app_uri)
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/gunicorn/util.py", line 359, in import_app
  mod = importlib.import_module(module)
File "/opt/python3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
  return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/srv/main.py", line 16, in <module>
  from flask import Flask, render_template,make_response,jsonify, abort, send_from_directory, request, redirect
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)
1
  • Importing itsdangerous.json is deprecated. Import Python’s json module instead. Feb 19, 2022 at 18:44

4 Answers 4

15

Flask 1.1.2 is set up to require itsdangerous >= 0.24. The latest released (itsdangerous) version (2.10) deprecated the JSON API.

To continue using Flask 1.1.2, you need to require at most itdangerous 2.0.1 (not 2.10). You can do this by adding

itsdangerous==2.0.1

...to your requirements.txt file (or wherever you list your dependencies).

itsdangerous 2.10 changelog

10

I just had the same issue and upgraded Flask to version 1.1.4, but then I got:

'soft_unicode' from 'markupsafe'

This issue seems to be related: ImportError: cannot import name 'soft_unicode' from 'markupsafe' in Release 1.38.0 #3661

Downgrading markupsafe to version 2.0.1 fixed it for me.

1
9

Either update Flask to 1.1.4, or fix itsdangerous version to 2.0.1

1
  • 1
    Is anyone by any chance also using connexion? I am, and I see pip using Flask 1.1.4 but the markupsafe problem remains. Just speculating that it might be from connexion? Feb 18, 2022 at 22:04
0

I found the same problem today.

I used Flask in version 1.1.2. The problem disappeared after an update to version 1.1.4.

1
  • I see the markupsafe problem with Flask 1.1.4; maybe I'm using some other package that pins the version differently from what Flask wants? Feb 18, 2022 at 22:03

You must log in to answer this question.