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に設定したものではなく、sw/binにあるapt-getを実行できません。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)