How to install multiple versions of Python using Anaconda

Install Anaconda

Note: It’s not good practice to share anaconda environments with other users. Make sure to make a backup of your startup file and that you launch a new terminal after the installation.  Example below is for bash.

cp -p ~/.profile ~/.profile.orig


You can find the latest version here: https://www.anaconda.com

Note that the graphical installer is the easiest to use although a command-line instruction is also available here https://docs.anaconda.com/free/anaconda/install/mac-os/

Make sure you allow Anaconda to modify your bash profile otherwise it will not work

Test your install of Anaconda by running  the following

conda --version


To update, make sure you are in your home directory

cd ~; conda update -n base -c defaults conda


Getting multiple versions of python

List the versions of python you available.

conda search python

Create an environment with that version.

#py27 is the name of the environment, you may name the environment anything.
#assuming we have python 3.7 and are trying to create an environment for python 2.7, run:

conda create -n py27 python=2.7 anaconda 

We now have two environments, the original environment with python 3.7, and a second one for 2.7

To list all available environment runs:

conda info --envs

or

conda env list


Switching between environments

To switch between environments, run the following

#Assuming py27 is the environment name, run:
conda activate /Users/john/anaconda3/envs/py27

#To switch back to the original environment, run:
conda deactivate

Activating and Deactivating Anaconda 

Instead of uninstalling anaconda, you can activate and deactivate Anaconda when needed.

Permanently

The following command will stop Anaconda from launching the base environment upon launching terminal and when opening new terminal windows.  

conda config --set auto_activate_base false

The Command below will re-enable base environment upon launching terminal and new terminal windows.

conda config --set auto_activate_base true

Reference: https://stackoverflow.com/questions/54429210/how-do-i-prevent-conda-from-activating-the-base-environment-by-default/54489016#54489016


Temporarily

The following commands will temporarily activate and deactivate Anaconda in a single terminal window. Opening a new terminal window will open in anaconda's base environment.
NOTE: The commands below only works on Conda 4.6 and newer. 

Activating Anaconda 

conda activate

Deactivating Anaconda 

conda deactivate

Activating Anaconda prior to 4.6

source activate

Deactivating Anaconda prior to 4.6

source deactivate


Deleting environments

https://iq.opengenus.org/delete-conda-environment/


Exporting and Importing Environments

Exporting environments 

conda env export > environment.yml


Importing environments

conda env create -f environment.yml


reference https://docs.anaconda.com/anaconda/user-guide/tasks/switch-environment/

To uninstall Anaconda

https://docs.anaconda.com/anaconda/install/uninstall/