0

I have carefully read answers from Setting the correct encoding when piping stdout in Python but:

  • the solution export PYTHONIOENCODING=utf8 does not work if you're not calling the process yourself. Example: here the process is called from Apache + mod_cgi, and running export PYTHONIOENCODING=utf8 in Bash does not change anything; I still have import sys; print(str(sys.stdout.encoding)) giving ANSI_X3.4-1968.

  • the solution

    import sys, codecs
    sys.stdout = codecs.getwriter('utf8')(sys.stdout)
    

    probably worked on Python 2.7 but not really for Python 3, because then, print(s) expects s being bytes, which is not very handy

Should we use a way to permanently export PYTHONIOENCODING=utf8 for all processes of the system (that is persistent, even after a reboot), if so how?

Basj
  • 41,386
  • 99
  • 383
  • 673

1 Answers1

0

After further research, the solution is to use SetEnv PYTHONIOENCODING utf8 in .htaccess files, as detailed here: mod_cgi + utf8 + Python3 produces no output.

For other processes it might be interesting to put PYTHONIOENCODING=utf8 in /etc/environment for persistence (not sure if it does the job for all processes that could call a Python script).

Basj
  • 41,386
  • 99
  • 383
  • 673