use conda environment in sublime text 3

Question:

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

Asked By: BolzanoW

||

Answers:

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.

Answered By: MattDMo

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

Answered By: MGLondon

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.

Answered By: Thom Ives

Coming across this same issue and using all of the information provided by the contributors to this post. My solution which provided the ease of switching virtual environments directly from Sublime Text 3:

Thom is correct to follow the guidelines of installing Conda for Sublime Text 3 provided by Anaconda’s documentation: https://docs.anaconda.com/anaconda/user-guide/tasks/integration/sublime/

Once completed, open the command palette in Sublime Text 3 (pressing CTRL+Shift+p (Windows, Linux) or CMD+Shift+p (macOS)) and type conda to select Conda: Activate Environment. You will at first notice it only contains the base environment which means the settings for the Conda package is not pointing to the correct Anaconda environment path.

To find the location of your virtual environment, for Mac in terminal, activate a virtual environment as you would normally do by typing conda activate myenv (myenv is the name of any environment you have already created in Anaconda), when activated then type echo $CONDA_PREFIX which will provide you with the location of the environment. For me, I found the location is

~/opt/anaconda3/envs/

Once you have located the path, go back to Sublime Text 3, go to Preferences -> Package Settings -> Conda -> Settings. This will bring up the Conda package settings where you can change the path to the "environment_directory". Save, shut down Sublime Text and restart. Now you can have any python file open and change the environment at any time by going to Command Pallate, select Conda: Activate Environment and select the environment from all virtual environments you have created within Anaconda.

Answered By: Wick 12c
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.