|
| 1 | +# Virtual Environments |
| 2 | + |
| 3 | +There are a few ways to manage virtual environments for Python development. |
| 4 | +The most popular are: |
| 5 | + |
| 6 | +- [venv](https://docs.python.org/3/library/venv.html) |
| 7 | +- [conda](https://docs.conda.io/en/latest/) |
| 8 | + |
| 9 | +In this guide we are going to learn how to use virtual environments to manage Python installation(s). |
| 10 | +Specifically, |
| 11 | +[conda environments](https://docs.conda.io/projects/conda/en/latest/user-guide/concepts/environments.html#conda-environments) |
| 12 | +managed with |
| 13 | +[micromamba](https://mamba.readthedocs.io/en/latest/user_guide/micromamba.html). |
| 14 | + |
| 15 | +## Why virtual environments? |
| 16 | + |
| 17 | +Python is ubiquitous, it's probably used in lots of different places on your computer already. |
| 18 | +Code written for specific versions of either the Python language or Python packages won't necessarily work with different versions. |
| 19 | + |
| 20 | +A virtual environment is a little box you can put Python and various packages into. |
| 21 | +The box is isolated from the rest of your system such that what you do inside the box won't affect what's going on outside the box. |
| 22 | + |
| 23 | +## Why *micromamba*? |
| 24 | + |
| 25 | +`micromamba` is a pure C++ reimplementation of `conda`. This makes it extremely fast and portable. |
| 26 | +One of the easiest ways to mess up a conda installation is to install a bunch of stuff into the base environment. |
| 27 | +`micromamba` doesn't give you a base environment, so you can't mess it up. |
| 28 | + |
| 29 | +## Install *micromamba* |
| 30 | + |
| 31 | +### Run the installer |
| 32 | +Install `micromamba`, the executable we will use to manage our virtual environments. |
| 33 | + |
| 34 | +=== "MacOS/Linux" |
| 35 | + |
| 36 | + Download and run the official micromamba installer. |
| 37 | + |
| 38 | + <div class="termy"> |
| 39 | + ```console |
| 40 | + $ "${SHELL}" <(curl -L micro.mamba.pm/install.sh) |
| 41 | + ---> 100% |
| 42 | + |
| 43 | + // You will be asked whether you want to initialise your shell. |
| 44 | + // Respond "Y". |
| 45 | + |
| 46 | + Init shell? [Y/n] Y |
| 47 | + |
| 48 | + // You will be asked where you want to install micromamba. |
| 49 | + // Using the default is recommended. |
| 50 | + |
| 51 | + Prefix location? [~/micromamba] |
| 52 | + |
| 53 | + // You will need to restart your shell for changes to take effect. |
| 54 | + |
| 55 | + Please restart your shell to activate micromamba. |
| 56 | + ``` |
| 57 | + |
| 58 | + </div> |
| 59 | + |
| 60 | + Next, setup `micromamba` to download packages from |
| 61 | + [*conda-forge*](https://conda-forge.org/) |
| 62 | + a community driven package repository. |
| 63 | + |
| 64 | + <div class="termy"> |
| 65 | + |
| 66 | + ```console |
| 67 | + $ micromamba config append channels conda-forge |
| 68 | + $ micromamba config append channels nodefaults |
| 69 | + $ micromamba config set channel_priority strict |
| 70 | + ``` |
| 71 | + |
| 72 | + </div> |
| 73 | + |
| 74 | +=== "Windows" |
| 75 | + |
| 76 | + todo: add windows guide |
| 77 | + |
| 78 | +### Set up an alias |
| 79 | + |
| 80 | +!!!tip "set up an alias for micromamba" |
| 81 | + `micromamba` replaces `conda`. Set up an alias if you want to type `conda` at the prompt. |
| 82 | + |
| 83 | +=== "bash" |
| 84 | + |
| 85 | + ```bash title="~/.bashrc" |
| 86 | + alias conda="micromamba" |
| 87 | + ``` |
| 88 | + |
| 89 | +=== "zsh" |
| 90 | + |
| 91 | + ```zsh title="~/.zshrc" |
| 92 | + alias conda="micromamba" |
| 93 | + ``` |
| 94 | + |
| 95 | +=== "PowerShell" |
| 96 | + |
| 97 | + ```PowerShell title="$Home\Documents\PowerShell\Microsoft.PowerShell_profile.ps1" |
| 98 | + Set-Alias conda mamba |
| 99 | + ``` |
| 100 | + |
| 101 | +We will use `conda` at the prompt rather than `micromamba` for the rest of this guide. |
| 102 | + |
| 103 | +## Usage |
| 104 | + |
| 105 | +### Creating and activating environments |
| 106 | + |
| 107 | +Run the following to create and activate an environment called `my-env` with Python 3.10 |
| 108 | + |
| 109 | +<div class="termy"> |
| 110 | + |
| 111 | + ```console |
| 112 | + $ conda create --name my-env python=3.10 |
| 113 | + |
| 114 | + // To work in the environment we first need to activate it. |
| 115 | + |
| 116 | + $ conda activate my-env |
| 117 | + |
| 118 | + // Your prompt will indicate your current environment. |
| 119 | + |
| 120 | + $ (my-env) ➜ |
| 121 | + |
| 122 | + // Let's check that we have the correct Python version. |
| 123 | + |
| 124 | + $ (my-env) ➜ python --version |
| 125 | + Python 3.10.10 |
| 126 | + |
| 127 | + ``` |
| 128 | + |
| 129 | +</div> |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | +### Installing packages |
| 134 | + |
| 135 | +You can install most packages into your environment with `conda` or `pip`. |
| 136 | +Install what you can with `conda`. If a package is available on |
| 137 | +[PyPI](https://pypi.org/) but not [conda-forge](https://conda-forge.org/) then use `pip`. |
| 138 | + |
| 139 | +=== "conda" |
| 140 | + |
| 141 | + <div class="termy"> |
| 142 | + |
| 143 | + ```console |
| 144 | + $ (my-env) ➜ conda install numpy |
| 145 | + ``` |
| 146 | + |
| 147 | + </div> |
| 148 | + |
| 149 | + |
| 150 | +=== "pip" |
| 151 | + |
| 152 | + <div class="termy"> |
| 153 | + |
| 154 | + ```console |
| 155 | + $ (my-env) ➜ pip install numpy |
| 156 | + ``` |
| 157 | + |
| 158 | + </div> |
| 159 | + |
| 160 | +### Deactivating environments |
| 161 | + |
| 162 | +We can deactivate an environment with |
| 163 | + |
| 164 | +<div class="termy"> |
| 165 | + |
| 166 | + ```console |
| 167 | + $ (my-env) ➜ conda deactivate |
| 168 | + |
| 169 | + // You are no longer in my-env. |
| 170 | + |
| 171 | + $ |
| 172 | + ``` |
| 173 | + |
| 174 | +</div> |
| 175 | + |
| 176 | +### Removing environments |
| 177 | + |
| 178 | +We can remove an environment with |
| 179 | + |
| 180 | +<div class="termy"> |
| 181 | + |
| 182 | + ```console |
| 183 | + $ conda env remove --name my-env |
| 184 | + |
| 185 | + // Your environment no longer exists |
| 186 | + |
| 187 | + $ conda activate my-env |
| 188 | + Cannot activate, prefix does not exist at: '/Users/pydev/micromamba/envs/ |
| 189 | + ``` |
| 190 | + |
| 191 | +</div> |
| 192 | + |
| 193 | +### Running from outside an environment |
| 194 | + |
| 195 | +You can run use software in a specific environment from outside the environment. |
| 196 | + |
| 197 | +<div class="termy"> |
| 198 | + |
| 199 | + ```console |
| 200 | + $ conda run --name my-env <command> |
| 201 | + ``` |
| 202 | + |
| 203 | +</div> |
| 204 | + |
| 205 | +This is useful for workflows which rely on software with incompatible dependencies. |
| 206 | + |
| 207 | +### Tips |
| 208 | + |
| 209 | +!!!tip "Get comfy! 🧸" |
| 210 | + Getting comfortable with the creation, destruction, activation and deactivation of environments at will is liberating. |
| 211 | + Practice now! |
| 212 | + |
| 213 | +!!!tip "One environment per project 🌍" |
| 214 | + Working with one environment per project is a useful ideal. |
| 215 | + A general purpose environment can be useful for quick scripts and analysis. |
| 216 | + |
| 217 | +## Closing |
| 218 | + |
| 219 | +That's it! Working in virtual environments empowers you to install things without |
| 220 | +worrying about messing up your whole system. |
0 commit comments