AttributeError in jinja2.ext.babel_extract with Python 3 #158

Zortaniac opened this Issue Oct 17, 2012 · 3 comments


None yet
3 participants

If you use Python 3, the babel_extract method fails, maybe because of the no longer existing decode method on string objects. (I use the python 3 compatible babel implementation from )

File "venv/lib/python3.2/site-packages/jinja2/", line 593, in babel_extract
source ='encoding', 'utf-8'))
AttributeError: 'str' object has no attribute 'decode'

Doing a little research on how this can be fixed in py3. It looks like python 3 needs to specify the encoding when you open the file (

a_file = open('examples/chinese.txt', encoding='utf-8')

But the issue is that in the file specified in this bug, the fileobj is passed into the babel extract function. It would be awesome to get a trace back or even better, a code block that causes the error, or even more better, a unit test. I don't know enough about this toolchain to reproduce this bug with the provided info.

The error causes in the extract_messages command of in my pyramid project. I have work around the error by commenting out the decode function after read(), but I think its required for python 2 and only correct as long as the decoding is utf-8.
I hope this traceback helps:

extracting messages from backend/templates/dashboard.jinja2 (encoding="utf-8")
Traceback (most recent call last):
File "", line 45, in
('**.py', 'chameleon_python', None ),
File "/usr/lib/python3.2/distutils/", line 148, in setup
File "/usr/lib/python3.2/distutils/", line 917, in run_commands
File "/usr/lib/python3.2/distutils/", line 936, in run_command
File "/venv/lib/python3.2/site-packages/Babel-1.0dev-py3.2.egg/babel/messages/", line 314, in run
for filename, lineno, message, comments, context in extracted:
File "/venv/lib/python3.2/site-packages/Babel-1.0dev-py3.2.egg/babel/messages/", line 171, in extract_from_dir
File "/venv/lib/python3.2/site-packages/Babel-1.0dev-py3.2.egg/babel/messages/", line 201, in extract_from_file
File "/venv/lib/python3.2/site-packages/Babel-1.0dev-py3.2.egg/babel/messages/", line 281, in extract
for lineno, funcname, messages, comments in results:
File "/venv/lib/python3.2/site-packages/jinja2/", line 593, in babel_extract
source ='encoding', 'utf-8'))
AttributeError: 'str' object has no attribute 'decode'


mitsuhiko commented May 20, 2013

The file object comes from babel. I would expect it to give me one in bytes. I would file that bug against babel and not Jinja.

@mitsuhiko mitsuhiko closed this May 20, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment