Sublime Text 2:自定义PATH和PYTHONPATH

时间:2011-12-20 11:47:21

标签: environment-variables development-environment sublimetext

我正在使用brew在/ usr / local / bin /中安装python(2.7.2) 但是,默认系统python(2.7.1)在/usr/bin/处执行,这似乎是因为它不遵守任何bash PATH环境变量。 此外,它找不到我的模块,因为它们安装在/usr/local/lib/python:/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages

我一直在使用Python.sublime-settings尝试以下操作,但它不起作用:

{
"path": "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin",
"env": ["PYTHONPATH", "/usr/local/lib/python:/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages"],
"cmd": ["python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}

如何让Sublime Text服从这些环境变量?

4 个答案:

答案 0 :(得分:62)

env需要是一个JSON对象,或者如果你愿意,你可以像这样:

"env":
{
    "PYTHONPATH":"/usr/local/lib/python:/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages"
},

答案 1 :(得分:14)

我通过以下方式设置我的路径系统来获得它:

## PATH
export PATH=/usr/local/bin:/usr/local/share/python:$PATH

## PYTHON
export PYTHONPATH=/usr/local/lib/python:$PYTHONPATH

# make systemwide
launchctl setenv PATH $PATH
launchctl setenv PYTHONPATH $PYTHONPATH

编辑: 该死的,这不适用于python,只是为了PYTHONPATH,当我尝试它时,它仍然给出了错误的python。用于检查python二进制位置的代码:

import sys, os
print os.path.dirname(sys.executable)

EDIT2: 通过在Python.sublime-build:

中硬链接到正确的python二进制文件来解决这个问题
{
    "cmd": ["/usr/local/bin/python", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
}

编辑3:

通过在脚本顶部打印os.environ,可以更轻松地调试PYTHONPATH变量问题。 Sublime Text 2变量名称显然不适用于'env'。

答案 2 :(得分:9)

很抱歉碰到一个旧帖子但是如果有人登陆这个页面寻找一种方法来使sublime2使用自定义$ PATH所以插件(例如一个shell插件)使用你当前的系统$ PATH这对我有用:

创建文件(插件):

~/Library/Application Support/Sublime Text 2/Packages/User/Any_ol_name.py

然后将此代码粘贴到:

import os

# Tweak line below as needed for your $PATH
LOCAL = '/usr/local/bin:/usr/local/sbin'

# Sublime's default path is
# /usr/bin:/bin:/usr/sbin:/sbin
# it'll be prepended to your custom one
os.environ['PATH'] += ':'
os.environ['PATH'] += LOCAL

print 'PATH = ' + os.environ['PATH']

Post with the original code here..

当你启动Sublime Text 2时会加载这个插件,我个人用它来运行shell命令,就像我从终端那样运行,修复一些由于路径变量错误而没有加载的插件。

答案 3 :(得分:0)

这是一篇非常古老的文章,但是我登陆这里寻找Sublime Text 3的解决方案。以防万一,人们也登陆这里,快速的解决方案是进入 Preferences.sublime-settings < / em> 并添加:

def invert_dicts(*dicts):
    """ Takes multiple dicts and returns a dict mapping
    key to dict index. E.g.,

    invert_dicts(
        {'a': 1, 'b': 2},
        {'a': 3, 'c': 4}
    )

    returns

    {'a': [0, 1], 'b': [0], 'c': [1]}
    """
    key_map = {}
    for i, d in enumerate(dicts):
        for k in d.keys():
            key_map.setdefault(k, []).append(i)
    return key_map


def weird_n_union(*dicts):
    """Applies the logic in OP's question to an arbitrary number of inputs

    >>> weird_n_union(d1, d2, ..., dn)

    Args:
        *dicts (dict): dictionaries w/one level of nested dicts as values

    Returns: dict

    """
    result = {}

    # dict mapping key to list of dict index in `dicts` containing key
    key_map = invert_dicts(*dicts)

    for k in key_map:

        # no outer key collision
        if len(key_map[k]) == 1:
            result[k] = dicts[key_map[k][0]][k]
        # outer key collision
        else:
            # unclear what should happen in the case where:
            # - there is an outer key collision
            # - there are no shared sub-keys
            #
            # this implementation assumes that in that case, the value for k is {}
            result.setdefault(k, {})

            sub_dicts = tuple(dicts[i][k] for i in key_map[k])

            # map keys in `sub_dicts` to indices for `dicts` containing key
            sub_key_map = invert_dicts(*sub_dicts)

            # contains elements of (k, v), where k appears in > 1 sub-dicts
            shared_keys_only = filter(lambda kv: len(kv[1]) > 1,
                                      sub_key_map.items())

            # update result with the max value for each shared key
            for kv in shared_keys_only:
                max_ = max(((kv[0], sub_dicts[i][kv[0]]) for i in kv[1]),
                           key=lambda x: x[1])
                result[k].update({max_[0]: max_[1]})

    return result