use conda environment in sublime text 3

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):

  1. Install Sublime Text 3
  2. Press Ctrl + Shift + P to open up the Command Palette
  3. Type "package" in the Command Palette search menu.
  4. From the options, choose "Install Package Control"
  5. Next after install, in the Command Palette type "Package Control: Install Package"
  6. Search for "conda" to find "Conda" with the description "Work with conda environments in Sublime Text 3"
  7. Navigate to "Preferences -> Package Settings -> Conda -> Settings-Default" to ensure the default settings such as Anaconda installation directory etc. are the same on your system. If they are not, open up "Preferences -> Package Settings -> Conda -> Settings-User", and copy the settings you'd like to update using the format shown in the default settings file as a template.
  8. Once installed, a Conda build system will appear in the build sytem menu and conda's commands will be located inside the command palette
  9. Choose the Conda build sytem by navigating to "Tools -> Build System -> Conda"
  10. Create a test file and save it as test.py with the following code in it:
x = 1
y = 2
print(x + y)
  1. Press Ctrl + B to build the file and see the output. If everything is working okay, you should see 3 as the output.
  2. If you get an error such as 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.


NOTE: This will work for virtual environments created with conda as well as venv or virtualenv, you just need to activate it first to find the path to the python[3] executable.

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.


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.