2017-05-08 83 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