命令通过python脚本失败但手动工作

时间:2013-01-02 05:01:57

标签: python

我遇到这个问题,下面的命令是通过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

2 个答案:

答案 0 :(得分:1)

由于lunchbuild/envsetup.sh中定义的bash函数,您可以在调用build/envsetup.sh之前创建一个源lunch 12的bash脚本,或者您可以{{1}执行bash命令,如

Popen

例如:

bash -c "source /tmp/envsetup.sh && lunch 12"

答案 1 :(得分:0)

你应该实际使用它:

import shlex
from subprocess import Popen

the_command = '/path/to/lunch 12'
result = Popen(shlex.split(the_command))

由于12lunch的参数而不是命令的一部分,shlex将自动处理拆分命令及其参数。

当你有一个命令和参数时,你应该传递Popen一个列表。确保你真的需要shell=True?你知道what it actually does吗?

相关问题