Redirect subprocess.Popen stderr to console
<p>I am executing a make command using subprocess.Popen. But when the make fails I do not get the exact error from make and th escript just continues to run. How do I get the script to stop and show the console exactly the output of the make command</p>

<pre><code>def app(self, build, soc, target):
    command = "make BUILD=%s SOC=%s TARGET=%s" % (build, soc, target)
    subprocess.Popen(command.split(), shell=False,
                              stdout=subprocess.PIPE,
                              stderr=subprocess.PIPE).communicate()
</code></pre>

<p>If you want the make output to actually go to the console, don't use <code>subprocess.PIPE</code> for stdout/stderr.  By default, the called process will use the Python process's stdout/stderr handles.  In that case, you can use the <code>subprocess.check_call()</code> function to raise a <code>subprocess.CalledProcessError</code> if the called process returns a non-zero exit code:</p>

<pre><code>subprocess.check_call(command.split())
</code></pre>

<p>However, if you need to capture the make output for use in your script, you can use the similar <code>subprocess.check_output()</code> function:</p>

<pre><code>try:
    output = subprocess.check_output(command.split(), stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
    output = e.output
    # error handling here
</code></pre>

<p>Note that this combines the stdout and stderr output into a single value.  If you need them separately, you would need to use the <code>subprocess.Popen</code> constructor in conjunction with the <code>.communicate()</code> method and manually checking the <code>returncode</code> attribute of the <code>Popen</code> object:</p>

<pre><code>p = subprocess.Popen(command.split(), stdout=subprocess.PIPE,
        stderr=subprocess.PIPE)
out, err = p.communicate()
if p.returncode != 0:
    # raise exception or other error handling here
</code></pre>

python