Using Sublime Text 3, how can I build a python file using a conda environment that I\'ve created as in http://conda.pydata.org/docs/using/envs.html
In Linux Mint, I kept having trouble getting sublime to run python scripts using Anaconda's environment and Anaconda's installed version of python. I was running the following script to check which python was being used:
import sys
print(sys.version)
I followed THIS procedure on the Anaconda site, but I had to do one additional thing to get sublime to use the Anaconda environment and run python scripts using its python environment.
After choosing "conda" as my build system, I had to access the Command Palette (Tools -> Command Palette ...), and then I typed "conda", which shows you all of the options for controlling conda from inside Sublime, and I had to chose "Conda: Activate Environment", which shows all "conda" environments that have been created. I only had the original environment at this point, so it only gave me that one choice. I chose it, and then my script used the Anaconda environment, and its python version correctly.
FURTHERMORE, I noticed that if I wanted to switch to another virtual environment that I had previously created on my system before using Anaconda, I did have to activate that environment from inside Sublime first. I could then use the build system choice "Python + Virtualenv" to use that activated environment. Fortunately, the conda environment was still activated, and I only needed to use the build system choice of "conda" to switch back to it.
You may use a package called "Conda" from the package repository. Below is a detailed step by step guide for the same (using Windows 10 OS PC, however it should work on other OSs in a similar way):
Ctrl + Shift + P
to open up the Command Palettex = 1
y = 2
print(x + y)
Ctrl + B
to build the file and see the output. If everything is working okay, you should see 3 as the output.error: [winerror 2] the system cannot find the file specified python
, it may mean that Anaconda has different settings on your computer than the default settings. In that case you would need to pass your computer settings to Sublime Text in "Preferences -> Package Settings -> Conda -> Settings-User": 1) Change "executable": "~\\Anaconda3\\python"
to the Anaconda python install location on your system, for example "executable": "Z:\\Anaconda3\\python.exe"
, 2) Change "environment_directory": "~\\Anaconda3\\envs\\"
to the default environment directory on your system, for example: "environment_directory": "Z:\\Anaconda3\\envs"
, 3) Change "configuration": "~\\.condarc"
to the path to conda's configuration file on your system for example configuration": "C:\Users\SantaPaws\.condarc"
Note 1: If you do not yet have a .condarc
on your system, open "Anaconda Prompt" and type conda config --write-default
. This would generate a .condarc
file and save it somewhere either on your home directory (C drive) or the Anaconda directory. Search the file using Windows search and find its location. Refer to https://conda.io/docs/user-guide/configuration/use-condarc.html for full instructions.
Note 2: You may need to update the default %PATH%
path variable in your system, so that it contains the directories for Anaconda. Type: echo %PATH%
both in the "Anaconda Prompt" and the windows cmd
prompt to see if these paths are the same, if not, you would need to update it in the windows system environment variable "Path". However, Anaconda recommends caution with doing this, as it can break other things.
A standard Python .sublime-build file looks like this:
{
"cmd": ["/path/to/python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
All you need to do to use a particular conda
environment is modify the path to the python
or python3
executable within the environment. To find it, activate your environment and type (depending on the version you're using)
which python
or
which python3
on Linux/macOS, or
where python
on Windows, then copy the path into your custom .sublime-build
file. Save the file in your Packages/User
directory, then make sure you pick the right one via Tools -> Build System
before building.