title: subprocess.run() argument encoding
body: <p>I have a Flask application (Linux, Apache with mod_wsgi, Python 3) which calls a shell script with some arguments. When there are any non-ascii characters in the <code>subprocess.run()</code> command arguments, following error occurs in the application:</p>
  
  <blockquote>
    <p>'ascii' codec can't encode characters in position 5-6: ordinal not in
    range(128)</p>
  </blockquote>
  
  <p>I spent a lot of time trying to fix it.</p>
  
  <p>No such problem exists in the command line, only in the application.</p>
  
  <p>The entire application's output is in Unicode and there are no problems with it. After some research I came to the conclusion the problem is with the "filesystem encoding".</p>
  
  <p>I have added some logging statements to my <code>run.wsgi</code> script. The FS encoding was 'ascii' indeed (and 'utf-8' in the command line).</p>
  
  <p>In the next step I found this article
  <a href="https://stackoverflow.com/questions/20172002/how-to-change-file-system-encoding-via-python">How to change file system encoding via python?</a></p>
  
  <p>The Apache httpd server was started with <code>LANG=C</code> in its environment. I have changed it to <code>C.UTF-8</code> despite warnings in <code>/etc/sysconfig/httpd</code>. That did not help, the FS encoding was still 'ascii'. I have then even monkey-patched the <code>sys.getfilesystemencoding()</code> to <code>lambda: 'utf-8'</code>. But the error is still there.</p>
  
  <p>I have properly restarted the httpd service after each change.</p>
  
  <p>I'm at my wits' end.</p>
  
  <ol>
  <li>Is my problem really caused by the FS encoding?</li>
  <li>If yes, why my attempts to change it to utf-8 failed?</li>
  <li>Most importantly: How can I solve this issue?</li>
  </ol>
  
  <p>UPDATE1:</p>
  
  <p>code snippet:</p>
  
  <pre><code>    import subprocess as sub
      cmdresult = sub.run(
          [SCRIPT, tid, days, name],
          stdin=sub.DEVNULL, stdout=sub.PIPE, stderr=sub.DEVNULL,
          encoding='ascii', # 'utf-8' will not help, this affects stdin, stdout I/O only
          check=True)
  </code></pre>