python:执行subprocess.popen时出错

时间:2017-05-08 19:24:50

标签: 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 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参数执行,但我仍然得到同样的错误。

2 个答案:

答案 0 :(得分:1)

当您的意思是yum -y install时,您正试图执行yum install -y

答案 1 :(得分:1)

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

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

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

但通常不鼓励这样做,for many reasons