我遇到这个问题,以下命令通过 python 脚本失败,如果我尝试在它通过的任何 linux 盒子上的命令行上手动运行此命令,只能通过它失败的脚本,任何输入这里是错误的还是调试提示?
source= Popen(['source build/envsetup.sh'],stdout=PIPE,stderr=PIPE, shell=True)
stdout,stderr=source.communicate()
print stdout
print stderr
lunchcommand=Popen(['lunch 12'],stderr=PIPE,shell=True)
stdout,stderr= lunchcommand.communicate()
print "Printing lunch stdout and stderr"
print stderr
/bin/sh: lunch: command not found
最佳答案
由于 lunch
是 build/envsetup.sh
中定义的 bash 函数,因此您可以创建一个源 build/envsetup.sh
的 bash 脚本code> 在调用 lunch 12
之前,或者您可以让 Popen
执行 bash 命令,例如
bash -c "source /tmp/envsetup.sh && lunch 12"
例如:
import subprocess
import shlex
with open('/tmp/envsetup.sh', 'w') as f:
f.write('function lunch() { KEY="$@"; firefox "www.google.com/search?q=${KEY}" ; }')
proc = subprocess.Popen(shlex.split('bash -c "source /tmp/envsetup.sh && lunch stackoverflow"'))
proc.communicate()
关于python - 命令通过 python 脚本失败但手动工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14117044/