{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Conda\n", "\n", "\n", "Conda is an open-source, cross-platform, language-agnostic package manager and environment management system ([Wikipedia](https://en.wikipedia.org/wiki/Conda_(package_manager))).\n", "It collects Python packages mainly from [Anaconda](https://www.anaconda.org/) and [conda-forge](https://github.com/conda-forge), but also from other channels for instance using PyPI.\n", "\n", "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. \n", "\n", "### Conda vs Python virtual environments\n", "\n", "Here is a summary table comparing conda and Python virtual environments ([link](https://docs.conda.io/projects/conda/en/latest/user-guide/concepts/environments.html)).\n", "\n", " \"Conda\n", "\n", "\n", "So, why use conda virtual environments?\n", "\n", "Mainly to **manage** packages and libraries from languages other than Python in the same space.\n", "\n", "But you can do this without conda as well. (In fact, I think that conda is often an overkill and is overhyped.)\n", "\n", "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 \n", "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 \n", "with your codes. \n", "\n", "\n", "### Conda virtual environment\n", "\n", "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.\n", "\n", "Two files are relevant here:\n", "\n", "- `requirements.txt` (see [here](https://github.com/borisbolliet/ResearchComputing/blob/main/docs/material/part10/requirements.txt))\n", "- `environment.yml` (see [here](https://github.com/borisbolliet/ResearchComputing/blob/main/docs/material/part10/environment.yml))\n", "\n", "In the `requirements.txt` file we list the Python packages we want to install in the environment, like we saw before. \n", "For instance:\n", "\n", "```\n", "numpy\n", "pandas==2.2.3\n", "scipy==1.14.1\n", "scikit-learn\n", "matplotlib>=3.9.2\n", "seaborn\n", "```\n", "\n", "where you can specify the version of the packages if you want (like we did for `pandas` and `scipy` or `matplotlib`).\n", "\n", "The `environment.yml` file is for other metadata of the environment, like the Python version.\n", "For instance:\n", "\n", "```yaml\n", "name: MphilC124\n", "channels:\n", " - defaults\n", " - conda-forge\n", "dependencies:\n", " - python=3.12\n", " - pip=24.2.0\n", " - pip:\n", " - -r requirements.txt\n", "```\n", "\n", "Channels are the sources of packages. `defaults` is the default channel of Anaconda, `conda-forge` is a community-driven channel.\n", "\n", "To create a conda virtual environment do:\n", "\n", "```bash\n", "conda env create -f environment.yml\n", "```\n", "\n", "from inside the directory where both files are.\n", "\n", "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. \n", "\n", "\n", "### Activate and deactivate the environment\n", "\n", "To activate the environment do:\n", "\n", "```bash\n", "conda activate MphilC124\n", "```\n", "\n", "To deactivate the environment do:\n", "\n", "```bash\n", "conda deactivate\n", "```\n", "\n", "from anywhere in your computer.\n", "\n", "### Installing more packages\n", "\n", "You can install more packages in the environment using `conda install` or `pip install`.\n", "\n", "### List all conda environments\n", "\n", "To list all environments do:\n", "\n", "```bash\n", "conda env list\n", "```\n", "\n", "### Remove a conda environment\n", "\n", "To remove a conda environment do:\n", "\n", "```bash\n", "conda env remove -n MphilC124\n", "```\n", "\n", "or delete the directory where the environment is stored.\n", "\n", "\n", "### Access your conda environment from Jupyter\n", "\n", "It works the same way as for Python virtual environments.\n", "\n", "First, activate the environment:\n", "\n", "```bash\n", "conda activate MphilC124\n", "```\n", "\n", "Then create a Jupyter kernel:\n", "\n", "```bash\n", "python -m ipykernel install --user --name MphilC124 --display-name \"MphilC124 (Python 3.12)\"\n", "```\n", "\n", "You may need to install `ipykernel` in the environment first:\n", "\n", "```bash\n", "conda install ipykernel\n", "```\n", "\n", "(or `pip install ipykernel`).\n", "\n", "You can then test you have the packages you asked for, for instance:\n", "\n" ] } ], "metadata": { "kernelspec": { "display_name": "MphilC124 (Python 3.12)", "language": "python", "name": "mphilc124" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.7" } }, "nbformat": 4, "nbformat_minor": 4 }