Running Python code: subprocess.check_output(), getting error: [Winerror 2]
Question: I'm running some Python code that is meant to run an Apache Maven program on a
file and produce an output:

    
    
    import os, subprocess
    os.chdir("C:/Users/Mohammad/Google Drive/PhD/Spyder workspace/production-consumption/logtool-examples/")
    logtoolDir = "C:/Users/Mohammad/Google Drive/PhD/Spyder workspace/production-consumption/logtool-examples/"
    processEnv = {'JAVA_HOME': 'C:/Program Files/Java/jdk1.8.0_66/jre',
              'mvn': 'C:/Program Files/apache-maven-3.3.3/bin/'}
    #processEnv = "C:/Program Files/Java/jdk1.8.0_66"
    args = 'org.powertac.logtool.example.ProductionConsumption D:/PowerTAC/Logs/2015/log/powertac-sim-1.state testrunoutput.data'
    subprocess.check_output(['mvn', ' exec:exec',
                                 ' -Dexec.args=' + args],
                                 env = processEnv,
                                 cwd = logtoolDir)
    

However, it gives me this error:

    
    
    File "C:/Users/Mohammad/Google Drive/PhD/Spyder workspace/production-consumption/test.py", line 25, in <module>
    cwd = logtoolDir)
    
    File "C:\WinPython-64bit-3.4.3.6\python-3.4.3.amd64\lib\subprocess.py", line 607, in check_output
    with Popen(*popenargs, stdout=PIPE, **kwargs) as process:
    
    File "C:\WinPython-64bit-3.4.3.6\python-3.4.3.amd64\lib\subprocess.py", line 859, in __init__
    restore_signals, start_new_session)
    
    File "C:\WinPython-64bit-3.4.3.6\python-3.4.3.amd64\lib\subprocess.py", line 1112, in _execute_child
    startupinfo)
    
    FileNotFoundError: [WinError 2] The system cannot find the file specified
    

I've investigated some things and I've narrowed it down to what I'm expecting
is _winapi.CreateProcess being unable to _find_ the Apache Maven installation
(and run the _mvn_ command). The installation is already in my path env
variables (the line runs just fine through CMD). It may also be true that I've
somehow defined the directories wrongly, but I fail to find a problem there...
Can anyone offer a suggestion on how to fix this issue?

Cheers.


Answer: You can try appending "shell = True" to the arguments passed to subprocess
like so:

    
    
    subprocess.check_output(['mvn', ' exec:exec',
                             ' -Dexec.args=' + args],
                             env = processEnv,
                             cwd = logtoolDir, shell = True)
    

Though as it says
[here](https://docs.python.org/2/library/subprocess.html#frequently-used-
arguments), this can be a security risk if the contents of the call can be
determined by an external source, but I can't tell whether that's the case
from the code you've given.

