A water boa for a python!
Two primary sources for installation of Python packages are the Python's official distribution and the Anaconda distribution.
Each of them use their own package managers (pip
and conda
respectively) to install and update packages as well as create virtual environments to support multiple versions of Python in the same machine.
Due to some advantages (package dependency analysis and math kernel optimization libraries) of the Anaconda distribution over the official Python one, by default, we will be using the Anaconda distribution unless specifically mentioned otherwise.
Originally aimed for data science and machine learning, the Anaconda Individual Edition is a free distribution for Python and R programming languages constituting of a large number of packages for both.
The package manager conda
analyzes the installed packages and verifies the dependencies making the installation breakdown-proof for existing projects.
A light-weight, minimal edition of the distribution is also available under the name Miniconda.
Download the graphical installer (.exe
/.pkg
) for the latest version of Anaconda Python from the individual product download page for the specific version of your operating system and run it.
Note: If Anaconda is the only distribution of Python in your system, make sure to check both "Add Anaconda to the system PATH environment variable" and "Register Anaconda as the system Python 3.x".
Before installing Anaconda, update the Debian build-tools by executing:
sudo apt-get install --upgrade build-essential
Download the command line installer (.sh
) for the latest version of Anaconda Python from the individual product download page. Alternatively, you can download the installer from the terminal using curl
(cURL) using the following commands:
sudo apt-get install --upgrade curl
curl -O https://repo.anaconda.com/archive/Anaconda3-2020.07-Linux-x86_64.sh
Install the downloaded file by running the following command in the terminal, replacing path/to/downloaded/file
with the complete path to the directory of the file along with the file name:
bash path/to/downloaded/file.sh
Note: Linux usually comes with official Python distributions accessible through python3
for version 3.x and python
for version 2.x.
Installing and initializing conda will override the default python
command in the terminal with Anaconda's version of Python.
conda-forge is a community-driven packaging system for conda dedicated for the scientific computing community. It provides a wide collection of recipies, build infrastructure and distributions. To add it as the first-hit channel, open the Anaconda command prompt in the elevated (administrator) mode and execute the following lines:
conda config --add channels conda-forge
conda config --set channel_priority strict
Initialize the conda environment using:
conda init
To verify the version of Python installed, run:
python --version
The output should be of the form:
Python 3.x.x
In Linux systems, the initialization scripts for conda
are available inside the $HOME/.bashrc
file.
It is useful to create individual virtual environments to run different versions of Python depending on the requirements of specific packages.
And the conda
package provides an easy way to manage such virtual environments.
To create a new environment (say, by the name py38
) supporting a particular version of Python (say Python 3.8.x), use the following command:
conda create --name py38 python=3.8
Once the creation is complete, you can activate the environment by:
conda activate py38
To remove the created environment, use the following command:
conda env remove --name py38
To install a package, use the following command by replacing package
with its name and version
with its requied version:
conda install package=version
To install multiple packages, append them one after the other separated by spaces.
To list all installed packages, use:
conda list
To update all packages, use:
conda update --all
To remove a package, use the following command by replacing package
with its name:
conda remove package
Conda's official cheat sheet collects a comprehensive list of commands available via the conda
package.