尝试使用 Eclipse 和 PyDev(调试)使以下脚本在 OSX 上运行:
#------------------------------------------------------
import os, subprocess
from os.path import join as join_path
def cmd(command):
print('$ ' + command)
process = subprocess.Popen(command, shell=True, executable="/bin/bash", stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return process.communicate() + (process.returncode,)
stdout, stderr, error_code = cmd('echo $PATH')
print(stdout, stderr, error_code)
stdout, stderr, error_code = cmd('echo $PYTHONPATH')
print(stdout, stderr, error_code)
stdout, stderr, error_code = cmd('which python')
print(stdout, stderr, error_code)
stdout, stderr, error_code = cmd('which apt-get')
print(stdout, stderr, error_code)
#------------------------------------------------------
但是 $PATH 不是我在 .bashrc 或 .profile 中设置的,我无法运行 apt-get,它位于 sw/bin 中。运行 subprocess.Popen 时,我的 $PATH 似乎被覆盖或设置不正确。
这是我上面脚本的输出:
$ echo $PATH
('/usr/bin:/bin:/usr/sbin:/sbin\n', '', 0)
$ echo $PYTHONPATH ('/Applications/eclipse/plugins/org.python.pydev_2.6.0.2012062515/pysrc/pydev_sitecustomize:/Users/bryancdickson/Development/Lootsie/_repos/ap/ap:/Users/bryancdickson/Development/Lootsie/_repos/ap/ap/ap:/sw/bin:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages:/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload:/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC:/Library/Python/2.7/site-packages\n', '', 0)
$ which python
('/usr/bin/python\n', '', 0)
$ which apt-get
> ('', '', 1)