subprocess.Popen() 在 Eclipse/PyCharm 和终端执行之间存在不一致的行为 [英] subprocess.Popen() has inconsistent behavior between Eclipse/PyCharm and terminal execution

查看:44
本文介绍了subprocess.Popen() 在 Eclipse/PyCharm 和终端执行之间存在不一致的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题是 Eclipse/PyCharm 以不同于标准终端的方式解释子进程的 Popen() 的结果.都在 OSX 上使用 python2.6.1.

The problem I'm having is with Eclipse/PyCharm interpreting the results of subprocess's Popen() differently from a standard terminal. All are using python2.6.1 on OSX.

这是一个简单的示例脚本:

Here's a simple example script:

import subprocess

args = ["/usr/bin/which", "git"]
print "Will execute %s" % " ".join(args)
try:
  p = subprocess.Popen(["/usr/bin/which", "git"], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  # tuple of StdOut, StdErr is the responses, so ..
  ret = p.communicate()
  if ret[0] == '' and ret[1] <> '':
    msg = "cmd %s failed: %s" % (fullcmd, ret[1])
    if fail_on_error:
      raise NameError(msg)
except OSError, e:
  print >>sys.stderr, "Execution failed:", e

使用标准终端,行:

ret = p.communicate()

给我:

(Pdb) print ret
('/usr/local/bin/git
', '')

Eclipse 和 PyCharm 给了我一个空元组:

Eclipse and PyCharm give me an empty tuple:

ret = {tuple} ('','')

更改 shell= 值也不能解决问题.在终端上,设置 shell=True,并完全传递命令(即 args=["/usr/bin/which git"])给我同样的结果:ret = ('/usr/local/bin/git ', '').Eclipse/PyCharm 都给了我一个空元组.

Changing the shell= value does not solve the problem either. On the terminal, setting shell=True, and passing the command in altogether (i.e., args=["/usr/bin/which git"]) gives me the same result: ret = ('/usr/local/bin/git ', ''). And Eclipse/PyCharm both give me an empty tuple.

对我可能做错了什么有任何想法吗?

Any ideas on what I could be doing wrong?

推荐答案

好的,找到问题了,在 Unix 类型的环境中使用 IDE 时要牢记这一点.IDE 的运行环境与终端用户不同(对吧?!).我没有考虑到子进程使用的环境与我的终端上下文不同(我的终端设置了 bash_profile 以在 PATH 中包含更多内容).

Ok, found the problem, and it's an important thing to keep in mind when using an IDE in a Unix-type environment. IDE's operate under a different environment context than the terminal user (duh, right?!). I was not considering that the subprocess was using a different environment than the context that I have for my terminal (my terminal has bash_profile set to have more things in PATH).

这很容易通过如下更改脚本来验证:

This is easily verified by changing the script as follows:

import subprocess
args = ["/usr/bin/which", "git"]
print "Current path is %s" % os.path.expandvars("$PATH")
try:
  p = subprocess.Popen(args, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  # tuple of StdOut, StdErr is the responses, so ..
  out, err = p.communicate()
  if err:
    msg = "cmd %s failed: %s" % (fullcmd, err)
except OSError, e:
  print >>sys.stderr, "Execution failed:", e

终端下,路径包括/usr/local/bin.在 IDE 下没有!

Under the terminal, the path includes /usr/local/bin. Under the IDE it does not!

这对我来说是一个重要的问题 - 永远记住环境!

This is an important gotcha for me - always remember about environments!

这篇关于subprocess.Popen() 在 Eclipse/PyCharm 和终端执行之间存在不一致的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆