python:执行subprocess.popen时出错

python:执行subprocess.popen时出错,python,subprocess,popen,Python,Subprocess,Popen,当我在centos中执行以下函数时,我得到一个错误 def install_requests_lib(): try: import requests return except ImportError, e: print "module does not exist, installing..." if(platform.system().lower()=='darwin'): print "install requ

当我在centos中执行以下函数时,我得到一个错误

def install_requests_lib():
   try:
      import requests
      return
   except ImportError, e:
      print "module does not exist, installing..."
      if(platform.system().lower()=='darwin'):
          print "install requests before proceeding, run **sudo pip install requests**"
          sys.exit(2)
      elif(platform.system().lower()=='linux'):
          print "installing"
          p=Popen(["yum","-y","install","python-requests"], stdout=PIPE, shell=True)
          p.communicate()
          print p.returncode
错误:

module does not exist, installing...
installing
You need to give some command
1
我想不出是怎么回事


我使用
stdin=PIPE
参数执行,但仍然得到相同的错误。

您正在尝试执行
yum-y安装
,当您的意思是
yum安装-y

如果您给出参数
shell=True
,则不会执行参数列表中
之后的参数。删除
shell=True
参数,它应该可以工作

或者,您可以将完整的命令行作为字符串提供,并保留
shell=True
参数:

p=Popen("yum install -y python-requests", stdout=PIPE, shell=True)

但通常不鼓励这样做。

您的脚本是否具有执行
yum安装
的权限?您还可以将stderr重定向到stdout以查看所有输出。