47

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?

kenorb
  • 155,785
  • 88
  • 678
  • 743
FLX
  • 4,634
  • 14
  • 47
  • 60
  • 1
    still can't make it work, did you find a solution in the meantime? – K.-Michael Aye Mar 28 '12 at 13:47
  • 2
    [docs for further details on build systems](http://docs.sublimetext.info/en/latest/reference/build_systems.html) – gorlum0 Mar 01 '13 at 07:33
  • For SublimeText3 build settings check out this answer: http://stackoverflow.com/questions/23789410/how-to-edit-sublime-text3-build-settings – Moberg Oct 27 '14 at 10:07

4 Answers4

62

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"
},
Bo Fjord Jensen
  • 769
  • 5
  • 8
  • 1
    Thanks, this helped me understand the format. I was able to fix my JAVA_HOME error with it like this: "env":{"JAVA_HOME":"$(/usr/libexec/java_home)"} – Mikko Tapionlinna Mar 14 '13 at 17:58
14

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

max
  • 2,346
  • 4
  • 26
  • 34
FLX
  • 4,634
  • 14
  • 47
  • 60
  • 1
    I added the PYTHON now in .bash_profile, including your launchctl commands and in the Environment.plist file, but STILL both TM2 and ST2 can not find my PYTHONPATH. I want to tear my hair out... :( – K.-Michael Aye Mar 28 '12 at 13:35
  • 1
    Edit2 works, you can also put in the PYTHONPATH under an "env" dictionary, like in Bo's answer, i.e. add a comma and a new line reading `"env":{"PYTHONPATH": "/whatever/your/pythonpath/is"}` before the last `}` – Markus Jun 27 '12 at 12:58
  • Tried all of this, still getting `NameError: name 'python' is not defined` – zakdances Jun 09 '13 at 22:47
9

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.

Ultriix
  • 99
  • 1
  • 1
  • This prints the correct PATH in Sublime's console, but it still can't find any commands like `python` or `coffee`. Still getting errors like `NameError: name 'python' is not defined` – zakdances Jun 09 '13 at 22:31
  • The last line should have parentheses around the string value, or you might get a syntax error: print ('PATH = ' + os.environ['PATH']) – Neil Monroe Oct 29 '14 at 19:36
  • @NeilMonroe -- I had a similar issue, but I believe it is because I was using sublimetext 3 -- which uses python 3, where print is a function, and so need the parens. – Kem Mason Nov 15 '14 at 02:18
0

This is a super old post but I landed here looking for this solution for Sublime Text 3. Just in case people land here as well, the quick solution is to go into Preferences.sublime-settings and add:

"additional_path_items": [
    "/path/to/add/1",
    "/path/to/add/2",
],
dave4jr
  • 1,131
  • 13
  • 18