Python 使用 subprocess 调用外部命令行程序报错

查看 86|回复 5
作者:ShihanW   
请问一下大家,我使用 python 编写了一个命令行工具,打包成.exe 后,已经将路径写入系统环境变量里了,在 cmd 里能全局调用,但是在另一个 pyhton 代码里使用 subprocess.check_ouput 调用这个工具,却报错了:returned non-zero exit status 1
这是什么原因导致的啊
python 版本:2.7
系统:winwos 10

Python, 调用, 序报, 命令

McZoden   
先把 stderr 重定向到 stdout 里,看一下是程序内部报错,还是环境问题,
参考:
https://stackoverflow.com/questions/16198546/get-exit-code-and-stderr-from-subprocess-call
shell=True 可能不需要,根据你原先代码来改
```
try:
output = subprocess.check_output(
cmnd, stderr=subprocess.STDOUT, shell=True, timeout=3,
universal_newlines=True)
except subprocess.CalledProcessError as exc:
print("Status : FAIL", exc.returncode, exc.output)
else:
print("Output: \n{}\n".format(output))
```
ShihanW
OP
  
@McZoden
哈喽, 我原先的代码就两行
```
cmd = "updateDoc -h"
result = subprocess.check_output(command, shell=True)
```
参照你给的修改示例,还是报错,这段代码是作为脚本在其他软件里调用的,不知道是不是版本不对,还提示没有 timeout 这个参数
McZoden   
2.7 的版本比较旧了,其他信息比较少,只能一步一步调试
先改成试试看吧,有没有什么其他的输出
cmd = "updateDoc -h"
result = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=True)
McZoden   
另外也可以试试 shell=False
ShihanW
OP
  
我在 2.7 版的 IDLE 里运行了上述代码,可以正常输出 subprocees.chek_output 结果,看起来问题是宿主程序导致的,非常感谢你的回复
您需要登录后才可以回帖 登录 | 立即注册

返回顶部