请问一下大家,我使用 python 编写了一个命令行工具,打包成.exe 后,已经将路径写入系统环境变量里了,在 cmd 里能全局调用,但是在另一个 pyhton 代码里使用 subprocess.check_ouput 调用这个工具,却报错了:returned non-zero exit status 1 这是什么原因导致的啊
python 版本:2.7 系统:winwos 10
1
McZoden 136 天前
先把 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)) ``` |
2
ShihanW OP @McZoden
哈喽, 我原先的代码就两行 ``` cmd = "updateDoc -h" result = subprocess.check_output(command, shell=True) ``` 参照你给的修改示例,还是报错,这段代码是作为脚本在其他软件里调用的,不知道是不是版本不对,还提示没有 timeout 这个参数 |
3
McZoden 136 天前
2.7 的版本比较旧了,其他信息比较少,只能一步一步调试
先改成试试看吧,有没有什么其他的输出 cmd = "updateDoc -h" result = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=True) |
4
McZoden 136 天前
另外也可以试试 shell=False
|
5
ShihanW OP 我在 2.7 版的 IDLE 里运行了上述代码,可以正常输出 subprocees.chek_output 结果,看起来问题是宿主程序导致的,非常感谢你的回复
|
6
julyclyde 135 天前
你可以在 subprocess 里输出一下 PATH 环境变量看看
应该是你 export 给 python ,但是 python 并没有进一步 export 给 subprocess 导致的吧 |