Python函数subprocess.check_output返回CalledProcessError:命令返回非零退出状态 [英] Python function subprocess.check_output returns CalledProcessError: command returns non-zero exit status

查看:531
本文介绍了Python函数subprocess.check_output返回CalledProcessError:命令返回非零退出状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为我之前的问题的快速解决,我很快在WinPython中运行以下Python代码:

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)

并出现以下错误:

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 

Apache Maven可执行文件似乎未运行.我的猜测是参数被错误地传递给程序.我在 args logtoolDir 参数中找不到任何错别字,但也许我在那里缺少什么?有什么想法吗?

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?

更新: mvn exec:exec未运行,因为check_output无法访问Windows的环境变量.我在processEnv中添加了 path 变量,现在check_output args中的'mvn','--version'确认了Maven运行.该代码仍然无法运行,但我想这可能与我如何定义目录有关.

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.

干杯.

推荐答案

问题已解决.基本上:a)subprocess.check_output无法读取Windows的环境变量(例如PATH,JAVA_HOME),因此我不得不重新定义我在processEnv中使用的变量并将其传递给函数的参数.同样,b) args 变量定义不正确.我需要删除一组引号,并使用r将其原始.

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.

更正后的代码:

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))

不幸的是,我找不到使用shell = True参数的方法,这可能不会成为问题,因为它仅用于数据分析.

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.

干杯.

这篇关于Python函数subprocess.check_output返回CalledProcessError:命令返回非零退出状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆