The need for setting up a virtual executing environment of Python has a lot of advantages and has been widely discussed over internet. Here I have a tutorial for setting up multiple environment with Anaconda and Sublime Text.
- The reason I choose Anaconda over Virtualenv is because Anaconda is widely used in the industry, especially in the big data and machine learning field, and it covers most functions of Virtualenv.
- I choose Sublime Text as my IDE because it is very simple yet powerful with many plugins. It is way faster and more efficient than many other IDES.
- The installation of Anaconda will not be covered here as there are many tutorials online already.
The tutorial has the following steps:
- Step 1: install Anaconda 3
- Step 2: setup python environment with Anaconda on Win 10
- Step 3: setup Sublime Text build file on Win 10
- Step 4: setup python environment with Anaconda on Mac
- Step 5: setup Sublime Text build file on Mac
- Summary of useful conda commands
Step 1: install Anaconda 3
- Install Anaconda 3. Choose the version that you use most frequently.
- For example, if you are working with python 3.6 most of the time, you should download the 3.6 version; otherwise, you can download the 2.7 version.
- If you encounter any difficulties during installation, google and stackoverflow are your best friend:)
Step 2: setup python environment with Anaconda on Win 10
- Open Anaconda Prompt to create new environment. Note that it is the Anaconda Prompt not the windows command line.
- When the Anaconda Prompt opens, it is in the default directory with many packages pre-installed. Now you can install the missing packages that you need using
conda install <package_name>
- For instance, you can install multiple packages at the same time using
conda install Django flask tornado twisted
- If there are some packages that cannot be installed using
conda
command, you can use pip
as described in the following steps.
- Once the packages are installed, you can create your own brand-new python 3.6 environment using
conda create -n py36 –clone base
as the following picture indicates.
- Since it is a clone from the base, all the packages you installed can be used here. This will save a lot of time.
- NOTE that you can also create the environment using
conda create -n <environment_name>
first and then install all the necessary packages
- Use
activate py36
to get into the environment you just created, and use pip to install the packages that cannot be installed with "conda".
- For example, wxpython can be install using pip command as follows (i.e.
pip install -U wxpython
):
- You can use
python
command to check the version of python and quit()
command to quit the python environment as follows:
- Use
deactivate
to quit current environment
- Now the python 3.6 environment is fully prepared, we can add the python 2.7 environment using
conda create -n py27 python=2.7
- Once the 2.7 environment is created, again you can use
activate py27
to get into the environment and use conda install <package_name>
or pip
to install packages as instructed above.
- When inside your base/root directory, you can use
conda info --envs
to check the current environments installed on your computer as follows:
Step 3: setup Sublime Text build file on Win 10
- Navigate to the New Build System
- Type the following JSON format code as follows. The path on the 4th line is the path to the python environment we created above.
- Save the file with an extension
.sublime-build
into the default folder
- Repeat steps 1 to 3 to setup for the other python environment.
- Now you can see the files you just saved have appeared in the "Build System".
- For example, I named as
py27
and py36
and they have appeared as shown in step 1
- Now you can check the versions of python by run different build using the following codes.
Run with Py36:
Run with Py27:
- NOTE: the anaconda path for Win 10 is usually
D:/Anaconda3/envs/<environemnt_name>/python
Step 4: setup python environment with Anaconda on Mac
- Setting up python with Anaconda on Mac is pretty much the same as on Win 10, except that when you activate the environment, you have to use command "source activate" rather than "activate".
- For package installation, it is identical to the steps for Win 10 described above. You can simply follow the steps in Step 2
Step 5: setup Sublime Text build file on Mac
- Navigate to the "New Build System"
- Find the Anaconda path of python environment. The following steps explains how to do it:
- Open a terminal, and activate the environment you would like to set up.
- For example,
source activate py27
- Use command
which python
to find out the path
- Don't close the terminal and remember the path. The above two steps canbe seen as follows:
- Use the path find above and type the following code as follows:
- Save it to the default location with the
.sublime-build
extension.
- You can repeat steps 1 to 4 for other environment you have created.
- To double check, run the following command and see the results:
- NOTE: the anaconda path for Mac is usually
/Users/<user_name>/anaconda3/envs/<environemnt_name>/bin/python
Summary of useful conda commands
Commands |
Description |
conda list |
list all the packages installed |
conda info --envs |
list all the environment created |
conda create -n *environment_name |
create an environment |
conda create --name *name1 --clone *name2 |
create a new environment (name1) to be a clone of another (name2) one |
conda install *package1 *package2 |
install multipe packages at the same time |
conda install *package = version |
install a package with a particular version |
- For more environment management commands, you can find them HERE