简体   繁体   English

python:执行subprocess.popen时出错

[英]python: error when executing subprocess.popen

when I execute the following function in centos, I get the error 当我在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

error: 错误:

module does not exist, installing...
installing
You need to give some command
1

I cannot figure out what is wrong. 我不知道怎么了。

I executed with stdin=PIPE argument, still I get the same error. 我执行了stdin=PIPE参数,但仍然收到相同的错误。

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

The arguments in your arg list after "yum" aren't being executed if you give the argument shell=True . 如果给定参数shell=True则不会执行arg列表中"yum"之后的参数。 Remove the shell=True argument and it should work. 删除shell=True参数,它应该可以工作。

Alternatively, you could supply the full command line as a string and keep the shell=True argument: 或者,您可以将完整的命令行作为字符串提供,并保留shell=True参数:

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

but it's generally discouraged to do so, for many reasons . 但是出于许多原因 ,通常不建议这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM