file.write() of non-ASCII characters differs in Interpreted Python than in script run

RAH rene.heymans at
Thu Aug 27 16:01:31 CEST 2015

Dear All,

The solution / explanation follows.

Thanks to Graham Dumpleton, the author of mod_wsgi (the WSGI module for Apache2) the source of the problem could be traced back to variables in Apache2. Below are the details reproduced from!topic/modwsgi/4wdfCOnMUkU

Now, everything is indeed UTF-8 !
Thanks again to each and everyone.

Best regards,


There is a file /etc/apache2/envvars referred to by /etc/apache2/apache2.conf.
In that file, I found the following lines:

## The locale used by some modules like mod_dav
export LANG=C
## Uncomment the following line to use the system default locale instead:
#. /etc/default/locale

As I don't need mod_dav, neither is it compiled with Apache2 ($apache2ctl -l), neither is it loaded with Apache2 ($apache2ctl -M), I commented / uncommented the 2 lines so that it now looks like:

#export LANG=C
. /etc/default/locale
export LANG

After a stop/start of Apache2, everything works fine and when I put the code:

from locale import getpreferredencoding
prefcoding = getpreferredencoding()
from os import environ
lang = environ["LANG"]
g = open('envresults', 'a')
g.write('LANG: ' + lang + '\n')
g.write('PrefCod: ' + prefcoding + '\n')

in my WSGI application, it gives me the same as the interpreter:

rse at Alibaba:~/test$ cat envresults
PrefCod: UTF-8
rse at Alibaba:~/test$

-*- The End -*-

More information about the Python-list mailing list