3. Conda

Conda is an open-source, cross-platform, language-agnostic package manager and environment management system (Wikipedia). It collects Python packages mainly from Anaconda and conda-forge, but also from other channels for instance using PyPI.

Working with conda is very similar to the workflow we presented for Python virtual environments (using pip and venv). We can create conda virtual environments.

3.1. Conda vs Python virtual environments

Here is a summary table comparing conda and Python virtual environments (link).

Conda vs Venv

So, why use conda virtual environments?

Mainly to manage packages and libraries from languages other than Python in the same space.

But you can do this without conda as well. (In fact, I think that conda is often an overkill and is overhyped.)

Actually, on remote computing cluster it is not recommended that you use conda, because non-Python libraries are installed in a system-wide way by the software researcher engineers of your institute and you should always use these rather than conda ones if you want best performance and if you want them to be able to help you with your codes.

3.2. Conda virtual environment

Conda is used by some of your lecturers and future colleagues, so let’s see how to work with it on a real life example.

Two files are relevant here:

  • requirements.txt (see here)

  • environment.yml (see here)

In the requirements.txt file we list the Python packages we want to install in the environment, like we saw before. For instance:

numpy
pandas==2.2.3
scipy==1.14.1
scikit-learn
matplotlib>=3.9.2
seaborn

where you can specify the version of the packages if you want (like we did for pandas and scipy or matplotlib).

The environment.yml file is for other metadata of the environment, like the Python version. For instance:

name: MphilC124
channels:
  - defaults
  - conda-forge
dependencies:
  - python=3.12
  - pip=24.2.0
  - pip:
    - -r requirements.txt

Channels are the sources of packages. defaults is the default channel of Anaconda, conda-forge is a community-driven channel.

To create a conda virtual environment do:

conda env create -f environment.yml

from inside the directory where both files are.

Some people name these files differently, for instance environment.yml and packages.txt, or env.yml and reqs.txt. Of course, you can name them as you please.

3.3. Activate and deactivate the environment

To activate the environment do:

conda activate MphilC124

To deactivate the environment do:

conda deactivate

from anywhere in your computer.

3.4. Installing more packages

You can install more packages in the environment using conda install or pip install.

3.5. List all conda environments

To list all environments do:

conda env list

3.6. Remove a conda environment

To remove a conda environment do:

conda env remove -n MphilC124

or delete the directory where the environment is stored.

3.7. Access your conda environment from Jupyter

It works the same way as for Python virtual environments.

First, activate the environment:

conda activate MphilC124

Then create a Jupyter kernel:

python -m ipykernel install --user --name MphilC124 --display-name "MphilC124 (Python 3.12)"

You may need to install ipykernel in the environment first:

conda install ipykernel

(or pip install ipykernel).

You can then test you have the packages you asked for, for instance: