6

So I've installed anaconda to a directory I have privileges for but I can't get sublime text 3 to recognise that the shell is now using anaconda python:

>which python
/local/home/USER/Apps/anaconda/bin/python

when I build with sublime launched from the same shell:

import astropy
print astropy.__file__

it gives a different directory: /soft/python-SL7/lib/python2.7/site-packages/astropy/init.pyc

My .tcshrc file reads:

setenv PATH /local/home/USER/Apps/anaconda/bin:${PATH}
alias subl /local/home/USER/Apps/sublime_text_3/sublime_text

My .bashrc (not that it should be using it) reads:

export PATH="/local/home/sread/Apps/anaconda/bin:$PATH"

Any ideas?

Lucidnonsense
  • 1,195
  • 3
  • 13
  • 35

2 Answers2

15

The easiest way is to create a new build system that points to your Anaconda installation. Create a new file in Sublime with JSON syntax and the following contents:

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

Save the file in your Packages/User directory (should be ~/.config/sublime-text-3/Packages/User) as Anaconda.sublime-build. Finally, select Tools → Build System → Anaconda, and when you hit CtrlB in a Python file it should now run using Anaconda.

If you want to set up SublimeREPL to use Anaconda with IPython in Sublime, you can follow the instructions here to set up the proper menu option (altering the path to suit your environment, of course), and my gist here for setting up SublimeREPL for IPython 4 and Jupyter.

Community
  • 1
  • 1
MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • That's not working for me. I copy-pasted your answer just to make sure. I've also replaced the cmd with `["which", "python"]` and `["which", "/local/home/USER/Apps/anaconda/bin/python"]. Both cmds return `/local/home/USER/Apps/anaconda/bin/python`. So the shell is correct it just keeps importing from `/soft/python-SL7/lib/python2.7/site-packages` – Lucidnonsense Oct 08 '15 at 08:22
  • Moreover, when I try `pip install astropy --update` it completes by saying it installed the previous version. Could this be related? – Lucidnonsense Oct 08 '15 at 13:07
  • @Lucidnonsense as far as `pip` is concerned, what does `pip -V` return? It should tell you which python executable it's associated with. For the other issue, are you setting a `PYTHONPATH` variable in your `.tcshrc` or `.bashrc`? – MattDMo Oct 08 '15 at 13:36
  • I've managed to fix this somehow. I don't know how. (`pip -V` returns anaconda path). Thanks – Lucidnonsense Oct 13 '15 at 19:26
  • I still can't get it to work. Probably sublime does not invoke the command "conda activate base". How could we do that? – kstn Nov 05 '21 at 23:46
  • @kstn use the [`Conda`](https://packagecontrol.io/packages/Conda) package. – MattDMo Nov 06 '21 at 19:19
1

The other answer is correct, but you can also have a per project setting by editing the project file and adding this:

"build_systems":
[
    {
        "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
        "name": "Anaconda Python Builder",
        "selector": "source.python",
        "shell_cmd": "\"python3\" -u \"$file\""
    }
],

This also has the advantage of not leaving too many build systems in the build menu.

Nicolay77
  • 2,085
  • 25
  • 20
  • This is entirely different. This is a build system automatically created by the [`Anaconda`](https://packagecontrol.io/packages/Anaconda) Sublime package, which is in no way related to the Anaconda Python distribution which the OP is using. – MattDMo Nov 06 '21 at 19:21