OSX + Eclipse + PyDev - PATH不正确

时间:2012-08-21 07:24:57

标签: python eclipse macos pydev apt-get

尝试使用Eclipse和PyDev(Debug)在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)

4 个答案:

答案 0 :(得分:2)

PyDev可能设置$ PATH,与bash配置文件中的设置无关。

我建议你:

从具有正确PATH设置的bash shell启动PyDev(我假设Eclipse将从shell继承环境)。

在Eclipse中显式配置PATH(我不知道如何执行此操作的详细信息,在文档中搜索“环境变量”)。

答案 1 :(得分:1)

好的,找到了解决问题的简单方法。在这里找到了许多好的笔记:Environment variables in Mac OS X

从终端启动Eclipse - 虽然令人讨厌 - 但运行正常。从我的终端类型/ Applications / eclipse / eclipse和我的环境变量中获取:

  

$ echo $ PATH   ('/ SW /斌:/ SW / sbin目录:在/ usr / bin中:/ bin中:/ usr / sbin目录:/ sbin目录:在/ usr / local / bin目录:在/ usr / X11 / bin中:在/ usr /本地/ MySQL的/ bin中:SW /斌:在/ usr /本地/ MySQL的/斌:SW /斌:在/ usr / X11R6 / bin中\ 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/蟒蛇:/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-老:/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:/sw/bin\n”,   '',0)$ python('/ usr / bin / python \ n','',0)$ apt-get   ('/ sw / bin / apt-get \ n','',0)

答案 2 :(得分:0)

您可以在以下位置找到内部PyDev首选项:

Window:Preferences:PyDev:Interpreter - Python

这包括系统PYTHONPATH。

答案 3 :(得分:0)

从终端启动Eclipse,使用Eclipse 4.3为我工作,但它不再适用于Eclise 4.5(火星)

问题是调用subprocess时没有正确设置PATH变量.Popen

解决方法是使用PATH

将真实os.environ添加到环境中
import os
os.environ['PATH'] = os.environ['PATH']+':'+os.getenv('PATH')

这适用于我(我只需要在Python解释器的环境中添加变量PATH及其值,请参阅首选项(首选项 - > PyDev - >解释器 - > Python解释器 - >环境)