蟒蛇:错误执行subprocess.popen时

问题描述 投票:1回答:2

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

python subprocess popen
2个回答
1
投票

在您的ARG列表中的参数"yum"后,如果你给的说法shell=True没有被执行。取出shell=True说法,它应该工作。

另外,您也可以在完整的命令行提供一个字符串,并保持shell=True参数:

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

但它一般不提倡这样做,for many reasons


1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.