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

标签 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

最佳答案

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

相关文章:

python - 如何在python中检查文件的字符数

android - Python SL4A 开发

python - 程序运行后如何保持在python交互式解释器中?

python - 从列表或元组创建新的 numpy 数组

python - 如何扩展字典的文件路径

python - Scapy - 在其他两个层之间插入数据包层

python:如何在字符串变量上使用 'import'?

python - PIL─Python Imaging Library─通过CherryPy将图像缩略图上传到SQLAlchemy数据库

python - pandas for loop,适用于小数据帧卡在大

python - 获取 Pandas 中 boolean 值的索引 - python