title: Python subprocess check_call, native way to print STDERR if it fails?
body: <p>I often wish to perform the action of <code>subprocess.check_output</code>, i.e. print STDOUT live, and check the return code is 0 on exit. However  if there is an error, I would like the STDERR to be printed. When looking at log files and just seeing the return code is often not very beneficial.</p>
  <p>E.g.:</p>
  <pre class="lang-py prettyprint-override"><code>subprocess.check_call(['ping', '-c', '1', 'github.com2'])
  </code></pre>
  <p>Will generate this error:</p>
  <pre><code>subprocess.CalledProcessError: Command '['ping', '-c', '1', 'github.com2']' returned non-zero exit status 2.
  </code></pre>
  <p>Here we are left wondering why it failed, on real world examples it becomes critical to be able to see what the actual error is. So I find I always have to wrap such calls in something like this to be able to see STDERR:</p>
  <pre><code>import subprocess
  cmds = ['ping', '-c', '1', 'github.com2']
  result = subprocess.run(cmds, stderr=subprocess.PIPE)
  if result.returncode != 0:
      msg = result.stderr.decode().strip()
      raise subprocess.CalledProcessError(f&quot;CALLED SUBPROCESS ERROR: Command: {' '.join(cmds)}\nReturn code {result.returncode}\nSTDERR: {msg}\n&quot;)
  </code></pre>
  <p>With the above one gets to see that STDERR is:</p>
  <pre><code>ping: github.com2: Temporary failure in name resolution
  </code></pre>
  <p>I feel there must be a built in way using subprocess, but can't figure it out.</p>
  <p>Im guessing it's not possible because CalledSubprocessError does not use STDERR is the string version of the exception:</p>
  <pre><code>class CalledProcessError(SubprocessError):
      &quot;&quot;&quot;Raised when run() is called with check=True and the process
      returns a non-zero exit status.
  
      Attributes:
        cmd, returncode, stdout, stderr, output
      &quot;&quot;&quot;
      def __init__(self, returncode, cmd, output=None, stderr=None):
          self.returncode = returncode
          self.cmd = cmd
          self.output = output
          self.stderr = stderr
  
      def __str__(self):
          if self.returncode and self.returncode &lt; 0:
              try:
                  return &quot;Command '%s' died with %r.&quot; % (
                          self.cmd, signal.Signals(-self.returncode))
              except ValueError:
                  return &quot;Command '%s' died with unknown signal %d.&quot; % (
                          self.cmd, -self.returncode)
          else:
              return &quot;Command '%s' returned non-zero exit status %d.&quot; % (
                      self.cmd, self.returncode)
  
      @property
      def stdout(self):
          &quot;&quot;&quot;Alias for output attribute, to match stderr&quot;&quot;&quot;
          return self.output
  
      @stdout.setter
      def stdout(self, value):
          # There's no obvious reason to set this, but allow it anyway so
          # .stdout is a transparent alias for .output
          self.output = value
  </code></pre>