Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sublime Text 2: custom PATH and PYTHONPATH

I'm using brew which installs python (2.7.2) in /usr/local/bin/ However, the default system python (2.7.1) is executed instead at /usr/bin/, which seems to be because it doesn't obey any of the bash PATH environment variables. Also, it can't find my modules, as they are installed at /usr/local/lib/python:/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages.

I've been trying the following with Python.sublime-settings, but it doesn't work:

{
"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"
}

How can I make Sublime Text obey these environment variables?

like image 883
FLX Avatar asked Dec 20 '11 11:12

FLX


3 Answers

env needs to be a JSON object, or dictionary if you will, like this:

"env":
{
    "PYTHONPATH":"/usr/local/lib/python:/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages"
},
like image 114
Bo Fjord Jensen Avatar answered Nov 01 '22 02:11

Bo Fjord Jensen


I got it by setting my paths system wide by doing the following:

## 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

Edit: Damn, this doesn't work for python, just for PYTHONPATH, when I try it, it still gives the wrong python. Code used to check python binary location:

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

Edit2: Fixed this by hardlinking to the right python binary in Python.sublime-build:

{
    "cmd": ["/usr/local/bin/python", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
}

Edit 3:

Debugging PYTHONPATH variable issues can be made easier by also printing os.environ at the top of your script. Sublime Text 2 variable names apparently do NOT work for 'env'.

like image 30
FLX Avatar answered Nov 01 '22 04:11

FLX


Sorry to bump an old post but if people land on this page looking for a way to make sublime2 use a custom $PATH so plugins (e.g a shell plugin) use your current systems $PATH this worked for me:

Create a file (plugin):

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

Then paste this code in:

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..

This plugin will load when you start Sublime Text 2, I personnally used it to run shell commands like I would from terminal and to fix a few plugins that werent loading due to bad path variable.

like image 9
Ultriix Avatar answered Nov 01 '22 02:11

Ultriix