python subprocess.call() cannot find Windows Bash.exe
<p>I have a program that gets output from another program which runs on the new windows subsystem for linux.  I have written a python program that runs from the windows system, but will execute the linux program using python subprocess module.  If this is confusing see the example below.</p>

<p>However, when I do this I find that when called through python subprocess, windows cannot find bash program.</p>

<p>example on the commandline or powershell in windows:</p>

<pre><code>C:\&gt;bash -c "echo hello world!"
hello world!

C:\&gt;python
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import subprocess as s
&gt;&gt;&gt; s.call('bash -c "echo hello world"'.split())
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "c:\Python27-32\lib\subprocess.py", line 524, in call
    return Popen(*popenargs, **kwargs).wait()
  File "c:\Python27-32\lib\subprocess.py", line 711, in __init__
    errread, errwrite)
  File "c:\Python27-32\lib\subprocess.py", line 948, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

&gt;&gt;&gt; s.call('bash -c "echo hello world"'.split(),shell=True)
    'bash' is not recognized as an internal or external command,
    operable program or batch file.
    1
</code></pre>

<p>I thought maybe it wasn't loading my path settings somehow so I put in the full address of the bash program.</p>

<pre><code>&gt;&gt;&gt; s.call(b,shell=True)
'C:\Windows\System32\bash.exe' is not recognized as an internal or external command,
operable program or batch file.
1
</code></pre>

<p>EDIT :  I realize my command might be giving a problem since o am splitting on spaces and the "echo hello world" is one argument, but trying the same thing with <code>bash -c ls</code> also gives the same error </p>

<p>For 32-bit programs running in the WOW64 subsystem, the "System32" directory <a href="https://msdn.microsoft.com/en-us/library/aa384187" rel="nofollow">gets redirected</a> to "SysWOW64". The WSL bash.exe loader is distributed as a 64-bit executable, so from 32-bit Python you need to use the virtual "SysNative" directory. For example:</p>

<pre><code>import os
import platform
import subprocess

is32bit = (platform.architecture()[0] == '32bit')
system32 = os.path.join(os.environ['SystemRoot'], 
                        'SysNative' if is32bit else 'System32')
bash = os.path.join(system32, 'bash.exe')

subprocess.check_call('"%s" -c "echo \'hello world\'"' % bash)
</code></pre>

<p>Note that currently Windows pipes aren't bridged to WSL pipes, so if you try to use <code>stdout=PIPE</code> or <code>subprocess.check_output</code>, the WSL bash loader will fail. You could read the console output directly via <code>ReadConsoleOutputCharacter</code> (e.g. see <a href="http://stackoverflow.com/a/38749458/205580">this answer</a>). Or, more simply, you can redirect output to a temporary file, passing the temporary file's path translated as a WSL path. For example:</p>

<pre><code>import tempfile

def wintolin(path):
    path = os.path.abspath(path)
    if path[1:2] == ':':
        drive = path[:1].lower()
        return '/mnt/' + drive + path[2:].replace('\\', '/')

cmd = '"%s" -c "echo \'hello world\' &gt; \'%s\'"'

with tempfile.NamedTemporaryFile(mode='r', encoding='utf-8') as f:
    subprocess.check_call(cmd % (bash, wintolin(f.name)))
    out = f.read()
</code></pre>

python windows bash subprocess