Python function subprocess.check_output returns CalledProcessError: command returns non-zero exit status
Question: As a follow-up to my previous question, which got solved quickly, I'm running
the Python code below in WinPython:

    
    
    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/'}
    args = r'"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],
                                 shell = True, cwd = logtoolDir)
    

And get the following error:

    
    
    CalledProcessError: Command '['mvn', 'exec:exec', '-Dexec.args="org.powertac.logtool.example.ProductionConsumption D:/PowerTAC/Logs/2015/log/powertac-sim-1.state testrunoutput.data"']' returned non-zero exit status 1 
    

The Apache Maven executable does not seem to run. My guess is that the
arguments are being passed on to the program incorrectly. I couldn't find any
typos in the _args_ or the _logtoolDir_ arguments, but maybe I'm missing
something there? Any ideas?

UPDATE: The mvn exec:exec was not running because `check_output` has somehow
been unable to access Windows' environmental variables. I added the _path_
variable to `processEnv` and now `'mvn','--version'` in the `check_output`
args confirms Maven runs. The code still doesn't run but I imagine it's
probably an issue with how I've defined the directories.

Cheers.


Answer: Problem solved. Basically: a) `subprocess.check_output` could not read
Windows' environment variables (e.g. PATH, JAVA_HOME), so I had to redefine
the one I was using in `processEnv` and pass it along in the function's
arguments. Also, b) The _args_ variable was defined incorrectly. I needed to
remove one set of quotation marks, and also make it raw using `r`.

The corrected code:

    
    
    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/',
              'Path' : 'C:/Program Files/apache-maven-3.3.3/bin/'}
    args = r"org.powertac.logtool.example.ProductionConsumption D:/PowerTAC/Logs/2015/log/powertac-sim-1.state testrunoutput2.data"
    print(subprocess.check_output(['mvn', 'exec:exec', '-Dexec.args='+ args],
                               shell = True, env = processEnv, cwd = logtoolDir))
    

Unfortunately I can't find a way to go around using the `shell = True`
argument, which probably won't be a problem since this will only be used for
data analysis.

Cheers.

