Why is my Flask app crashing with ImportError: cannot import name 'contextfunction' from 'jinja2'

Why is my Flask app crashing with ImportError: cannot import name 'contextfunction' from 'jinja2'

我在 Azure 上部署了一个 Flask 应用程序。在不更改任何依赖项的情况下,我突然收到此错误,ImportError:无法从 'jinja2' 导入名称 'contextfunction'。似乎与 Flask-admin 有关:

[2022-03-27 07:22:27 +0000] [7] [INFO] Starting gunicorn 20.1.0 
[2022-03-27 07:22:27 +0000] [7] [INFO] Using worker: geventwebsocket.gunicorn.workers.GeventWebSocketWorker 
File "/usr/local/lib/python3.10/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker   
[2022-03-27 07:22:03 +0000] [7] [INFO] Shutting down: Master    
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import 
from .base import expose, expose_plugview, Admin, BaseView, AdminIndexView # noqa: F401 
File "/usr/local/lib/python3.10/site-packages/flask_admin/helpers.py", line 2, in <module>  
File "/app/eccho.py", line 4, in <module>   
File "/usr/local/lib/python3.10/site-packages/gunicorn/app/base.py", line 67, in wsgi   
File "/usr/local/lib/python3.10/site-packages/gunicorn/util.py", line 359, in import_app    
[2022-03-27 07:21:47 +0000] [7] [INFO] Shutting down: Master    
return self.load_wsgiapp()  
File "/app/app/__init__.py", line 9, in <module>    
File "/usr/local/lib/python3.10/site-packages/gunicorn/workers/ggevent.py", line 146, in init_process   
[2022-03-27 07:22:03 +0000] [7] [INFO] Starting gunicorn 20.1.0 
from flask_admin import babel   
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load  
File "/app/eccho.py", line 4, in <module>   
[2022-03-27 07:22:03 +0000] [7] [INFO] Reason: Worker failed to boot.   
from .helpers import get_current_view   
File "<frozen importlib._bootstrap_external>", line 883, in exec_module 
from .base import expose, expose_plugview, Admin, BaseView, AdminIndexView # noqa: F401 
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked   
File "<frozen importlib._bootstrap_external>", line 883, in exec_module 
return util.import_app(self.app_uri)    
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked 
ImportError: cannot import name 'contextfunction' from 'jinja2' (/usr/local/lib/python3.10/site-packages/jinja2/__init__.py)    
File "/usr/local/lib/python3.10/site-packages/gunicorn/util.py", line 359, in import_app    
File "/usr/local/lib/python3.10/site-packages/flask_admin/__init__.py", line 6, in <module>

有人可以帮忙吗?

谢谢

contextfunction 在 Jinja2 3.0.0 中被 pass_context 取代,所以这绝对是一个依赖问题。你可以降级 Jinja 版本还是应该 运行 python 2 这个应用程序?