2017-05-08 88 views
1

时,当我在CentOS的执行下面的功能,我得到错误蟒蛇:错误执行subprocess.popen

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

+1

您的脚本是否有权执行'yum install'?您也可以将stderr重定向到stdout以查看所有输出。 – grundic

回答

1

如果您提供参数shell=True,则在"yum"之后的参数列表中的参数未被执行。删除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