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

2

10

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

Carbo answered 27/3, 2022 at 7:33 Comment(0)
A
10

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?

Aurangzeb answered 27/3, 2022 at 7:41 Comment(2)
Thanks @Johan, isn't Jinja2 automatically installed by Flask? So wouldn't I have to downgrade flask itself?....and would this downgrade Jinja2 with it? ...This is most definitely a Python 3 app.Carbo
No I don't think so, from flask.palletsprojects.com/en/2.0.x/templating "but you still have to install Jinja2 to run Flask itself". Here's the dependency list for flask in the install instructions: flask.palletsprojects.com/en/2.0.x/installation.Aurangzeb
F
-1

I uninstalled flask and jinja2 and then installed flask, it worked for me.

pip uninstall flask
pip uninstall jinja2
pip install flask
Forefinger answered 4/1, 2023 at 13:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.