flaskjinja2flask-admin

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


I have a Flask app deployed on Azure. Without changing any of the dependencies I'm suddenly getting this error, ImportError: cannot import name 'contextfunction' from 'jinja2'. Seems to be related to 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>

Can anyone help?

Thanks


Solution

  • contextfunction was replaced by pass_context in Jinja2 3.0.0 so it's definitely a dependency problem. Can you downgrade the Jinja version or should you be running python 2 for this app?