无法使用Jenkins运行Python子进程调用

问题描述 投票:0回答:1

该问题已在其他地方提出,但我看不到任何解决方案...

我有一个Python脚本,其中包含以下形式的子流程调用:

source_density = subprocess.check_output(["adb", device_dhm1, "-P8080", "-s", source, "shell", "wm density | head -2 | tail -1"])

通过cmd运行此脚本时,一切运行正常。但是现在我试图通过Jenkins运行它,并且得到以下错误输出:

======================================================================
ERROR: test_sanity (__main__.ABC)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/user123/.jenkins/workspace/Test/Jenkins-Source-Sanity.py", line 145, in test_sanity
    source_density = subprocess.check_output(["adb", device_dhm1, "-P8080", "-s", source, "shell", "wm density | head -2 | tail -1"])
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 216, in check_output
    process = Popen(stdout=PIPE, *popenargs, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 394, in __init__
    errread, errwrite)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1047, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory
python jenkins subprocess
1个回答
1
投票

默认情况下,check_output将第一个参数视为单个可执行文件,而不是shell命令(cmd命令)。因此,您必须显式设置标志shell=True

source_density = subprocess.check_output(["adb", device_dhm1, "-P8080", "-s", source, "shell", "wm density | head -2 | tail -1"], shell=True)

请阅读文档here

© www.soinside.com 2019 - 2024. All rights reserved.