错误“从 itsdangerous 导入 json 作为 _json ImportError:无法从‘itsdangerous’导入名称‘json’”

错误“从 itsdangerous 导入 json 作为 _json ImportError:无法从‘itsdangerous’导入名称‘json’”

在部署烧瓶适用于谷歌云平台,我收到此错误:

[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

Flask 1.1.2 是设置为要求这很危险>= 0.24. 最新发布的(这很危险) 版本(2.10)弃用了 JSON API。

要继续使用 Flask 1.1.2,你最多需要很危险2.0.1(不是 2.10)。您可以通过添加

itsdangerous==2.0.1

...到你的要求.txt文件(或者列出依赖项的任何位置)。

这很危险2.10 更新日志

答案2

我刚刚遇到了同样的问题,并将 Flask 升级到版本 1.1.4,但随后我得到了:

来自“markupsafe”的“soft_unicode”

这个问题似乎与以下相关:ImportError:无法从版本 1.38.0 中的“markupsafe”导入名称“soft_unicode”#3661

将 markupsafe 降级到版本 2.0.1 对我来说解决了这个问题。

答案3

要么更新烧瓶1.1.4或修复这很危险版本2.0.1

答案4

我今天发现了同样的问题。

我使用的是 1.1.2 版本的 Flask。更新到 1.1.4 版本后,问题消失。

相关内容