km457
km457

Reputation: 125

KeyError: 'babel' - related to __init__.py - Not using Babel explicitly

I've been getting the same error for hours, no matter what code changes I make, and it's referencing Babel, a dependency I am not specifically calling or using, although I assume it's being called for a reason. It's coming from a function that is supposed to use flask-table to render some HTML dynamically to make a table.

2018-06-08 21:30:12,121: [2018-06-08 21:30:12,114] ERROR in app: Exception on /checkin_home/ [POST]
2018-06-08 21:30:12,121: Traceback (most recent call last):
2018-06-08 21:30:12,121:   File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1982, in wsgi_app
2018-06-08 21:30:12,121:     response = self.full_dispatch_request()
2018-06-08 21:30:12,121:   File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1614, in full_dispatch_request
2018-06-08 21:30:12,122:     rv = self.handle_user_exception(e)
2018-06-08 21:30:12,122:   File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1517, in handle_user_exception
2018-06-08 21:30:12,122:     reraise(exc_type, exc_value, tb)
2018-06-08 21:30:12,122:   File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
2018-06-08 21:30:12,122:     raise value
2018-06-08 21:30:12,122:   File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1612, in full_dispatch_request
2018-06-08 21:30:12,122:     rv = self.dispatch_request()
2018-06-08 21:30:12,122:   File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1598, in dispatch_request
2018-06-08 21:30:12,123:     return self.view_functions[rule.endpoint](**req.view_args)
2018-06-08 21:30:12,123:   File "/my/working_dir/path/mysite/project/mci.py", line 49, in checkin_home_post
2018-06-08 21:30:12,123:     return checkin_search_module.checkin_search(event_guid=event_guid, input_string=input_string)
2018-06-08 21:30:12,123:   File "/my/working_dir/path/mysite/project/checkin_search_module.py", line 3, in checkin_search
2018-06-08 21:30:12,123:     from flask_table import Table, Col
2018-06-08 21:30:12,123:   File "/my/working_dir/path/.local/lib/python3.6/site-packages/flask_table/__init__.py", line 1, in <module>
2018-06-08 21:30:12,123:     from .table import Table, create_table
2018-06-08 21:30:12,123:   File "/my/working_dir/path/.local/lib/python3.6/site-packages/flask_table/table.py", line 8, in <module>
2018-06-08 21:30:12,124:     from .columns import Col
2018-06-08 21:30:12,124:   File "/my/working_dir/path/.local/lib/python3.6/site-packages/flask_table/columns.py", line 161, in <module>
2018-06-08 21:30:12,124:     class BoolCol(OptCol):
2018-06-08 21:30:12,124:   File "/my/working_dir/path/.local/lib/python3.6/site-packages/flask_table/columns.py", line 166, in BoolCol
2018-06-08 21:30:12,124:     yes_display = _('Yes')
2018-06-08 21:30:12,124:   File "/usr/local/lib/python3.6/dist-packages/flask_babel/__init__.py", line 539, in gettext
2018-06-08 21:30:12,124:     t = get_translations()
2018-06-08 21:30:12,124:   File "/usr/local/lib/python3.6/dist-packages/flask_babel/__init__.py", line 214, in get_translations
2018-06-08 21:30:12,124:     babel = current_app.extensions['babel']
2018-06-08 21:30:12,125: KeyError: 'babel'

On a basic level, the error seems to be: it's looking for a key babel in current_app.extensions and not finding it.

I tried reinstalling flask-table via pip (which has Babel as a dependency) and it was up do date, and I also tried reinstalling Babel directly, just in case, and it was also up to date. This is Python 3.6.

There are some more specific references further up to args and imports specific to my code, in lines 15-25 of that error, but if I run this same code in a console, it's fine, works as expected. It's just when I put it in Flask that it doesn't work. The rest of the application and routes function normally, it's only when I POST to checkin_home that I get the 500 error above.

From the research I've been able to do, it seems to have something to do with the way __init__.py in the given directory is being handled by the "application context" but I honestly don't really know what that means or how to fix it. All I know is it's a file in modules to tell python they're modules. I have another Flask app that renders a similar HTML table in the same environment that works fine.

I can post an approximation of the code, but it will take a ton of obfuscation of sensitive info, so before I do, I'd like to know if it's possible this is something simple I'm just not seeing.

Upvotes: 2

Views: 1666

Answers (2)

user40183
user40183

Reputation: 21

Try installing flask-babel and doing the following in your app initialization

from flask_babel import Babel

babel = Babel(app)

and retry.

Upvotes: 2

Randy Stegner Sr.
Randy Stegner Sr.

Reputation: 87

In my case, I had a tables.py file with

from flask_table import Table, Col, LinkCol

which worked fine when I created the app but as it grew I had to move things around. It became a large Flask app at that point and I decided to restructure everything in order to follow best practices. I followed several recommendations for structuring it with most of it coming from https://exploreflask.com/en/latest/index.html. I am therefore using the init.py file for creating the application factory.

To correct the error I was receiving I simply used the same import statement in the init file. It appears it is necessary to have it in both places. I'm not sure if this is the correct answer, but there were no other answers posted and this worked for me. If anyone with more experience feels otherwise, please politely correct me.

Upvotes: 2

Related Questions