I'd like the answers to accumulate a checklist of general reasons why unicode_literals
may not work as expected
from __future__ import unicode_literals
This feature has both good and bad consequences. It has no effect in Python 3. For Python 2 it makes all string literals unicode by default, without the u''
prefixes.
This feature is not working in my specific case in a flask file, which begins:
# encoding: utf-8
from __future__ import unicode_literals
from __future__ import print_function
assert isinstance('123', unicode)
This assert passes when I run from the command line. But fails when run by apache. (That is, this assert failure message appears in my error_log.) My goal is to have unicode strings everywhere.
Here are a few aspects unique to my particular case that could conceivably contribute:
- Python 2.7.11, Flask 0.11, Apache 2.2.15
the above .py file is named directly in my
WSGIScriptAlias
directive, instead of an intermediate .wsgi file as in the examples.I'm executing activate_this.py for a virtualenv before I import flask. I don't think that matters, as it comes later in the file but here are those lines
ACTIVATE_THIS_PY = '/somewhere/somewhere/activate_this.py' execfile(ACTIVATE_THIS_PY, dict(__file__=ACTIVATE_THIS_PY)) import flask
Related:
- Add headers in a Flask app with unicode_literals. Haven't found my wsgi version yet but this seems to be an old bug long ago fixed.