Needing a system that allows for easy switching of Python versions or, within one version, switching between different environments, Conda is the easiest way to go.
Conda is a package and environment management system, that was exactly developed for this purpose. With Anaconda being a collection of Python packages we are set to go. The installation is straight forward and does not require root rights. Select and download the appropriate installer from the Anaconda download page.
Select the correct platform (Linux in my case) and select the appropriate package below. I use the Python 3.7 Version 64-Bit (x86) Installer. The Python version does not matter too much, as we will later also install other versions using Conda.
When you have downloaded the file (in my case Anaconda3-2019.07-Linux-x86_64.sh), change to the download directory and make it executable
chmod u+x Anaconda3-2019.07-Linux-x86_64.sh
and run it:
The installation process is self-explanatory. First you have to accept the licence (hit enter untill you reach the end of the agreement, and confirm with ‘yes’), then you can choose a different installation directory if you wish (default will be located in $HOME/anaconda3). Finally you can initialize your Anaconda installation, which is deactivated by default – type ‘yes’ here so all neccessary initializations are added to your bash startup script. Finally close your terminal and start a new one (or just initialize the shell inside it) so Conda is available.
If you only want a single, custom Python installation, you are done here. If you want to switch between installations, read on…
Now, as we don’t want the base installation to be activated every time you start a shell, we deactivate the base installation startup:
conda config --set auto_activate_base false
Close and re-open the terminal to see the effect of that change.
Now we need to create our Python environments for the desired Python versions. To do so, you call
conda create -n py27 python=2.7 anaconda
It will present you a list of Python packages to be installed and ask for confirmation – press ‘Enter’ to accept and continue. This downloads all neccessary packages and creates a Python 2.7 environment called ‘py27’ with the commonly used packages installed (anaconda gives a preselection of packages). We do the same for Python 3.7:
conda create -n py37 python=3.7 anaconda
and you are done!
Now to work with either Python 2.7 or Python 3.7, you simply type
conda activate py27
conda activate py37
in order to activate the py27 or py37 environments. To deactivate the environments, type
at the command line.
Now as I find these commands a bit lengthy and I want to have the py27 environment activated by default, I added the following lines to my $HOME/.bashrc:
alias py27='conda deactivate; conda activate py27' alias py37='conda deactivate; conda activate py37' conda activate py27
This allows me to just type
in order to quickly switch environments. And has already the Python 2.7 environment activated by default.
Installing Python packages
Another advantage of using Conda is that we have full control of the Python packages that are installed and over the versions of these. This is very much similar to the approach by using virtualenv. While your Conda environment is active, in order to install e.g. the AstroPy package you just type
conda install astropy
or, if you want a specific version
conda install astropy=1.2
to install Astropy version 1.2.
Although Anaconda provides a large range of packages, not all Python packages are available through Conda. You might have to use PIP to install your desired package, when it is only available through the PyPI archive. This was the case for me e.g. for the pysqlite package, wich I use for Sqlite Database access. This you can simply install calling
pip install pysqlite
and it gets installed into your active Conda environment.
In order to keep your Conda/Anaconda installation and environments up to date you can update them.
You update Conda by calling
conda update conda
when you have no active environment.
In order to update all packages in an active environment, you run
conda update --all
and it will update all packages, taking care no dependency is broken (wich means not all packages might be upgraded to the latest version).
If you have packages installed using pip, you will need to update those, too. Type e.g.
pip install --upgrade pysqlite
to update the pysqlite package.