Is there a good reason why I shouldn't start all my python programs with this? Is there something special lost when doing exec like this?
#!/usr/bin/python
import os, sys
if sys.stdout.encoding == None:
os.putenv("PYTHONIOENCODING",'UTF-8')
os.execv(sys.executable,['python']+sys.argv)
print sys.stdout.encoding
There are 60 questions about PYTHONIOENCODING so I guess it's a common problem, but in case you don't know, this is done because when sys.stdout.encoding == None
then you can only print ascii chars, so e.g. print "åäö"
will throw an exception..
EDIT This happens to me when stdout is a pipe; python encoding.py|cat
will set encoding to None
Another solution is to change the codec of stdout sys.stdout = codecs.getwriter('utf8')(sys.stdout)
which I'm guessing is the correct answer dispite the comments on that question.