简体   繁体   English

运行Python代码:subprocess.check_output(),出现错误:[Winerror 2]

[英]Running Python code: subprocess.check_output(), getting error: [Winerror 2]

I'm running some Python code that is meant to run an Apache Maven program on a file and produce an output: 我正在运行一些Python代码,该代码旨在在文件上运行Apache Maven程序并产生输出:

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). 我已经研究了一些问题,并将其范围缩小到了我期望的_winapi.CreateProcess无法找到 Apache Maven安装(并运行mvn命令)。 The installation is already in my path env variables (the line runs just fine through CMD). 安装已经在我的环境env变量中了(该行通过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. 干杯。

You can try appending "shell = True" to the arguments passed to subprocess like so: 您可以尝试将“ shell = True”附加到传递给子流程的参数上,如下所示:

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

Though as it says here , 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. 虽然,因为它说在这里 ,这可能是如果通话的内容可以由外部源确定存在安全隐患,但我不能告诉这是否是从你给的代码的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM