|
| 1 | +--- |
| 2 | +title: How to set up a virtual environment |
| 3 | +--- |
| 4 | + |
| 5 | +In this guide we summarize some key commands to set up a virtual environment |
| 6 | +with different tools in the scientific python ecosystem. |
| 7 | +A virtual environment is a workspace into which you can install Python |
| 8 | +libraries, separate from what is being used by your operating system. |
| 9 | + |
| 10 | +The environment managers that are covered in this how-to guide include: |
| 11 | +- venv |
| 12 | +- conda |
| 13 | +- mamba |
| 14 | +- uv |
| 15 | +- pixi |
| 16 | + |
| 17 | +In each of these examples we'll create a new virtual environment related to our project called `science` |
| 18 | +(you can use whichever name you prefer!). We'll activate the environment, install some dependencies, and see |
| 19 | +an example of installing dependencies from a file. |
| 20 | + |
| 21 | +### Set up a virtual environment with venv |
| 22 | + |
| 23 | +With venv we'll have our virtual environment associated with our project folder called `science`. |
| 24 | + |
| 25 | +``` |
| 26 | +python -m venv science |
| 27 | +``` |
| 28 | + |
| 29 | +Start using it by activating it as follows: |
| 30 | + |
| 31 | +``` |
| 32 | +source science/bin/activate |
| 33 | +``` |
| 34 | + |
| 35 | +You are now ready to install Scientific Python packages using `pip`! For example: |
| 36 | + |
| 37 | +``` |
| 38 | +pip install ipython numpy scipy |
| 39 | +``` |
| 40 | + |
| 41 | +Often you'll interact with projects that have a specific list of dependencies (for development |
| 42 | +environments, testing environments, or the project itself). You can install the list of dependencies |
| 43 | +with pip in your venv using: |
| 44 | + |
| 45 | +``` |
| 46 | +pip install -r requirements.txt |
| 47 | +``` |
| 48 | + |
| 49 | +Remember to re-activate your environment every time you open a new terminal, using: |
| 50 | + |
| 51 | +``` |
| 52 | +source science/bin/activate |
| 53 | +``` |
| 54 | + |
| 55 | +### Set up a virtual environment using conda |
| 56 | + |
| 57 | +With conda, we can create a new environment named science (-n is the same as passing --name): |
| 58 | + |
| 59 | +``` |
| 60 | +conda create -n science |
| 61 | +``` |
| 62 | + |
| 63 | +Start using your environment by activating it: |
| 64 | + |
| 65 | +``` |
| 66 | +conda activate science |
| 67 | +``` |
| 68 | + |
| 69 | +You are now ready to install Scientific Python packages using `conda`! |
| 70 | +For example: |
| 71 | + |
| 72 | +``` |
| 73 | +conda install ipython numpy scipy |
| 74 | +``` |
| 75 | + |
| 76 | +Often you'll interact with projects that have a specific list of dependencies (for development |
| 77 | +environments, testing environments, or the project itself). You can install the list of dependencies |
| 78 | +with conda in your environment using: |
| 79 | + |
| 80 | +``` |
| 81 | +conda install --file requirements.txt |
| 82 | +``` |
| 83 | + |
| 84 | +Remember to re-activate your environment every time you open a new terminal: |
| 85 | + |
| 86 | +``` |
| 87 | +conda activate science |
| 88 | +``` |
| 89 | + |
| 90 | +### Set up a virtual environment using mamba |
| 91 | + |
| 92 | +With mamba, like conda, we can create a new environment named science (-n is the same as passing --name): |
| 93 | + |
| 94 | +``` |
| 95 | +mamba create -n science |
| 96 | +``` |
| 97 | + |
| 98 | +Start using your environment by activating it: |
| 99 | + |
| 100 | +``` |
| 101 | +mamba activate science |
| 102 | +``` |
| 103 | + |
| 104 | +You are now ready to install Scientific Python packages using `mamba`! |
| 105 | +For example: |
| 106 | + |
| 107 | +``` |
| 108 | +mamba install ipython numpy scipy |
| 109 | +``` |
| 110 | + |
| 111 | +Often you'll interact with projects that have a specific list of dependencies (for development |
| 112 | +environments, testing environments, or the project itself). You can install the list of dependencies |
| 113 | +with mamba in your environment using: |
| 114 | + |
| 115 | +``` |
| 116 | +mamba install --file requirements.txt |
| 117 | +``` |
| 118 | + |
| 119 | +Remember to re-activate your environment every time you open a new terminal: |
| 120 | + |
| 121 | +``` |
| 122 | +mamba activate science |
| 123 | +``` |
0 commit comments