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

- 100,794
- 21
- 241
- 231
-
7Use `where python` on Windows. – Todd Oct 21 '17 at 01:37
-
2if you name the .sublime-build file in directory `\Sublime Text 3\Packages\User` as "pytouch.sublime-build", you would be able to choose **`Tools -> Build System -> pytouch`**, hence making it clearer for multi-environment development. – Wong Tat Yau Dec 18 '18 at 09:02
-
1Use `where python` on Windows with CMD and `gcm python` or `Get-Command python` within the Powershell. – tobiasraabe Mar 20 '20 at 09:47
-
I've done this in Windows but it points me to the same python executable as is used in the base environment... so how does this differentiate the environment? I'm trying to resolve the issue that my packages aren't loading correctly because the environment isn't loaded. – jremedy May 11 '22 at 10:04
-
@jremedy I'm not sure I understand - what exactly is "*I've done this in Windows*"? – MattDMo May 11 '22 at 17:37
-
I ran `where python` from cmd in Windows and it didn't recognize it. However, I resolved this issue upon realizing I had to give the absolute path for the where executable. – jremedy May 13 '22 at 05:26
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):
- Install Sublime Text 3
- Press
Ctrl + Shift + P
to open up the Command Palette - Type "package" in the Command Palette search menu.
- From the options, choose "Install Package Control"
- Next after install, in the Command Palette type "Package Control: Install Package"
- Search for "conda" to find "Conda" with the description "Work with conda environments in Sublime Text 3"
- 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.
- Once installed, a Conda build system will appear in the build sytem menu and conda's commands will be located inside the command palette
- Choose the Conda build sytem by navigating to "Tools -> Build System -> Conda"
- Create a test file and save it as test.py with the following code in it:
x = 1
y = 2
print(x + y)
- Press
Ctrl + B
to build the file and see the output. If everything is working okay, you should see 3 as the output. - 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.
-
3I had to do the step as below by @Thorn Ives: `Conda: Activate Environment` in order to make it work – Marcus V. Feb 06 '20 at 09:47
-
@MarcusV. Do you also have to activate the environment again every time you start Sublime? – Pontis Dec 12 '20 at 07:15
-
1@theBridge Yes, but I find this alright, as it makes it explicit for me to choose the right environment. And also in a terminal, you also explicitly activate an environment. So can live with that step. – Marcus V. Dec 14 '20 at 07:35
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.

- 3,642
- 3
- 30
- 29
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.