gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 19:07:52 26 4
gpt4 key购买 nike

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

最佳答案

由于 lunchbuild/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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com