1

尝试使用 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)
4

4 回答 4

2

PyDev 可能会独立于 bash 配置文件中的设置设置 $PATH。

我建议你:

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

或者

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

于 2012-08-21T07:39:28.563 回答
1

好的,想出了一个简单的解决方案。在这里找到了一些很好的笔记:Mac OS X 中的环境变量

从终端启动 Eclipse - 虽然很烦人 - 工作正常。从我的终端类型 /Applications/eclipse/eclipse 中获取我的环境变量:

$ echo $PATH ('/sw/bin:/sw/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/local /mysql/bin:sw/bin:/usr/local/mysql/bin:sw/bin:/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/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:/sw/bin\n', '', 0) $ which python ('/usr /bin/python\n', '', 0) $ which apt-get ('/sw/bin/apt-get\n', '', 0)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) $ which python ('/usr/bin/python\n', '', 0) $ which apt-get ('/sw/bin/apt-get\n', '' , 0)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) $ which python ('/usr/bin/python\n', '', 0) $ which apt-get ('/sw/bin/apt-get\n', '' , 0)

于 2012-08-21T18:00:23.833 回答
0

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

窗口:首选项:PyDev:解释器 - Python

这包括系统 PYTHONPATH。

于 2012-08-21T08:36:33.560 回答
0

从终端启动 Eclipse 对我来说适用于 Eclipse 4.3,但它不再适用于 Eclise 4.5 (Mars)

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

一种解决方法是使用将真实添加PATH到环境中os.environ

import os
os.environ['PATH'] = os.environ['PATH']+':'+os.getenv('PATH')

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

于 2015-07-29T12:38:52.940 回答