I am doing this:
import sys, codecs
sys.stdout = codecs.getwriter('utf8')(sys.stdout)
sys.stderr = codecs.getwriter('utf8')(sys.stderr)
But I know there is something missing. I had a huge collection of code before an HD crash, and my snippet in there had something in it which prevented:
reload(sys)
From undoing the codec changes. Does anyone know what that might have been?
Quite apart from UTF8 (and thus entirely apart from your Q’s title),
reload(sys)does not reopen stdandard input, output, and error files. Try a simpler case to see that:If you want to restore
sys.stdoutand/orsys.stderrafter rebinding them, just stash away the old references and just rebind them to the “stashed away” refs to restore. (You could usesys.__stdout__andsys.__stderr__, where Python itself stashes them away at startup, but that might run athwart advanced shells or debuggers you might be running, as the latter may be doing their own similar operations).If you also want to survive file or file-descriptor closures, you’ll have to go deeper (reopening e.g. with
os.fdopenthe standard descriptors 0, 1, and 2 — if the descriptors themselves have been closer, that gets even hairier;-) and living peacefully with advanced shells or debuggers in general may become a lost cause. But for simpler cases, just rebinding things back and forth can work fine.