subprocess.Popen() has inconsistent behavior between Eclipse/PyCharm and terminal execution

后端 未结 1 1444
执笔经年
执笔经年 2020-12-20 17:00

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.

相关标签:
1条回答
  • 2020-12-20 17:46

    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
    

    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!

    0 讨论(0)
提交回复
热议问题