|
26 | 26 | "## Why Use Virtual Environments?\n",
|
27 | 27 | "\n",
|
28 | 28 | "1. **Dependency Management** \n",
|
29 |
| - " Different projects might require different versions of the same library. A virtual environment allows each project to maintain its own set of dependencies.\n", |
| 29 | + " Different projects might require different versions of python or different versions of a particular library in order to work. A virtual environment allows each project to maintain its own set of dependencies, completely separate from other projects.\n", |
30 | 30 | "\n",
|
31 | 31 | "2. **Reproducibility** \n",
|
32 | 32 | " Sharing code is easier when collaborators can recreate the exact environment you used, avoiding the “it works on my machine” problem.\n",
|
33 | 33 | "\n",
|
34 |
| - "3. **Conflict Avoidance** \n", |
35 |
| - " If one project needs `libraryX==1.2.0` and another needs `libraryX==2.0.0`, separate virtual environments prevent these requirements from clashing.\n", |
36 |
| - "\n", |
37 |
| - "4. **System Integrity** \n", |
| 34 | + "3. **System Integrity** \n", |
38 | 35 | " Installing or updating packages system-wide can break other applications. Virtual environments confine these changes to the environment, leaving system Python untouched.\n",
|
39 | 36 | "\n",
|
40 | 37 | "## Using venv across different operating systems \n",
|
|
64 | 61 | ":align: center\n",
|
65 | 62 | "```\n",
|
66 | 63 | "\n",
|
| 64 | + "Commands can then be typed directly into the terminal and executed by pressing Return. \n", |
| 65 | + "\n", |
67 | 66 | "#### Verifying Python Installation\n",
|
68 | 67 | "\n",
|
69 | 68 | "You can verify that Python is installed correctly by running: \n",
|
70 | 69 | "\n",
|
71 | 70 | "```bash\n",
|
72 |
| - "python\n", |
| 71 | + "python --version\n", |
73 | 72 | "```\n",
|
| 73 | + "This should produce a output stating the installed python version, for example:\n", |
74 | 74 | "\n",
|
75 | 75 | "```bash\n",
|
76 |
| - "python3\n", |
77 |
| - "```\n", |
78 |
| - "\n", |
79 |
| - "depending on the version of python that you have installed, Python 2 or 3. When running the relevant above command it will take you to an interactive Python session, where you can define variables and print them such as with the following commands:\n", |
80 |
| - "\n", |
81 |
| - "```python\n", |
82 |
| - "x = 3 \n", |
| 76 | + "Python 3.12.4\n", |
83 | 77 | "```\n",
|
84 | 78 | "\n",
|
85 |
| - "```python\n", |
86 |
| - "print(x)\n", |
87 |
| - "```\n", |
| 79 | + "Minor differences in the python version (beyond the first `.`) will not matter for the most part. However, unless you have a specific reason to use Python 2, the version should start with a 3. If it does not or if Python is not found on your system then you can install it via the [official website](https://www.python.org/downloads/).\n", |
88 | 80 | "\n",
|
89 |
| - "If when running these commands you produce the following output: \n", |
| 81 | + "Typically Python is invoked by calling python followed by the path to a python script file, containing some code: \n", |
90 | 82 | "\n",
|
91 |
| - "```python\n", |
92 |
| - "3\n", |
| 83 | + "```bash\n", |
| 84 | + "python example_script.py\n", |
93 | 85 | "```\n",
|
| 86 | + "Calling Python from command line with no other arguments will open the [interpreter](https://docs.python.org/3/tutorial/interpreter.html) (indicated by the `>>>` prompt), where python commands can be executed in an interactive manner. This is rarely used as the code written is cleared when the interpreter is exited, which can be done by executing the `exit()` command: \n", |
94 | 87 | "\n",
|
95 |
| - "then you have confirmed that you have a working version of Python installed. \n", |
96 |
| - "\n", |
97 |
| - "You can exit the interactive session with the following command: \n", |
98 |
| - "\n", |
99 |
| - "```python\n", |
100 |
| - "exit\n", |
| 88 | + "```bash\n", |
| 89 | + ">>> exit()\n", |
101 | 90 | "```\n",
|
102 | 91 | "\n",
|
103 |
| - "If you don't have Python installed currently, then you are able to install it via the [official website](https://www.python.org/downloads/).\n", |
104 |
| - "\n", |
105 | 92 | "#### Checking what is currently installed\n",
|
106 |
| - "\n", |
107 |
| - "You can check which packages are currently installed with either of the following commands, again depending on if you have Python 2 or 3 installed. \n", |
108 |
| - "\n", |
| 93 | + "Python comes with some default packages installed already, and installation of packages is typically managed through PIP (a recursive acronym for \"pip install package\"). We can see what is currently installed system-wide using:\n", |
109 | 94 | "```bash\n",
|
110 | 95 | "pip list \n",
|
111 | 96 | "```\n",
|
112 |
| - "\n", |
113 |
| - "```bash\n", |
114 |
| - "pip3 list\n", |
115 |
| - "```\n", |
| 97 | + "we _could_ install more packages system-wide using pip, but it is better to do so in a virtual environment.\n", |
116 | 98 | "\n",
|
117 | 99 | "#### Creating an environment \n",
|
118 | 100 | "\n",
|
119 | 101 | "An environment can be created with the following command: \n",
|
120 | 102 | "\n",
|
121 | 103 | "```bash\n",
|
122 |
| - "python3 -m venv virtual_environment_1\n", |
| 104 | + "python -m venv virtual_environment_1\n", |
123 | 105 | "```\n",
|
124 | 106 | "\n",
|
125 | 107 | "On macOS this can then be activated with:\n",
|
|
142 | 124 | "deactivate\n",
|
143 | 125 | "```\n",
|
144 | 126 | "\n",
|
145 |
| - "which will remove the environment hint from the command line, highlighting that you are no longer in any environment. \n", |
| 127 | + "which will remove the environment hint from the command line, highlighting that you are no longer in any virtual environment. \n", |
146 | 128 | "\n",
|
147 |
| - "You would now be able to create a second virtual environment, called `virtual_environment_2` with the following commands: \n", |
| 129 | + "You would now be able to create a and activate a second virtual environment, called `virtual_environment_2` with the following commands: \n", |
148 | 130 | "\n",
|
149 | 131 | "```bash\n",
|
150 |
| - "python3 -m venv virtual_environment_2\n", |
| 132 | + "python -m venv virtual_environment_2\n", |
151 | 133 | "source virtual_environment_2/bin/activate\n",
|
152 | 134 | "```\n",
|
153 | 135 | "\n",
|
154 | 136 | "and easily switch between the two of them by first using `deactivate` and then `source <environment_name>/bin/activate`\n",
|
155 | 137 | "\n",
|
156 | 138 | "#### Installing a package\n",
|
157 | 139 | "\n",
|
158 |
| - "When we go to install a package it can be a good idea to check which packages are already installed which can be done with either of the following command depending on your python version: \n", |
| 140 | + "Whith a virtual environment active, when we go to install a package it can be a good idea to check which packages are already installed. This can be done with the following command: \n", |
159 | 141 | "\n",
|
160 | 142 | "```bash\n",
|
161 | 143 | "pip list \n",
|
162 | 144 | "```\n",
|
163 |
| - "or\n", |
164 |
| - "```bash\n", |
165 |
| - "pip3 list\n", |
166 |
| - "```\n", |
167 |
| - "\n", |
168 |
| - "This should give you a list of different python packages that are current installed. If you wanted to install [Pandas](https://pandas.pydata.org/), a very widespread Python Data Analysis Library then you could run the following command:\n", |
| 145 | + "This should give you a list of different python packages that are current installed. If you wanted to install a new package, [Pandas](https://pandas.pydata.org/) for example (a very widespread Python Data Analysis Library), then you could run the following command:\n", |
169 | 146 | "\n",
|
170 | 147 | "```bash\n",
|
171 |
| - "pip3 install pandas\n", |
172 |
| - "```\n", |
173 |
| - "or \n", |
174 |
| - "```bash\n", |
175 | 148 | "pip install pandas\n",
|
176 | 149 | "```\n",
|
177 | 150 | "\n",
|
178 |
| - "Now when you check which packages are install using `pip list` you should see that pandas is installed and can be used within this environment in your python scripts with the use of `import pandas` in this case. If you were to now run `deactivate` and `pip list` again you would see that the pandas packages is not listed. This is the key goal of virtual environments, allowing you to easily swap between environments where different combinations, and even version of packages are installed. For example it might be the case that on one of your projects you need to use Package X, but that only works with Package Y Version 1. However, there is a new release of Package Y Version 2 that has some novel features that you are interested in using for another project. Virtual environments allow you to install these package combinations once in different environments and then quickly switch between them using the process described above without having to perform the time-intensive install and uninstall process each time you switch between working on the projects. \n", |
| 151 | + "Now when you check which packages are install using `pip list` you should see that pandas is installed and can be imported into code written with this environment active using `import pandas` (feel free to try this in the interactive shell). If you were to now run `deactivate` and `pip list` again you would see that the pandas packages is not listed. \n", |
| 152 | + "\n", |
| 153 | + "This is the key goal of virtual environments: allowing you to easily swap between different configurations of packages and their respective versions without uninstalling and reinstalling everything. For example it might be the case that on one of your projects you need to use `pandas 2.2.2`, but that this is incompatible with some other package used in a different project, which only works with `pandas 1.8.0`. Using virtual environments allows each project to install the working version of pandas without replacing the version installed in the other project. \n", |
179 | 154 | "\n",
|
180 | 155 | "### Windows \n",
|
181 | 156 | "\n",
|
|
222 | 197 | "\n",
|
223 | 198 | "In your Command Prompt or PowerShell, run \n",
|
224 | 199 | "\n",
|
225 |
| - "\n", |
226 |
| - "```shell\n", |
227 |
| - "python\n", |
228 |
| - "```\n", |
229 |
| - "\n", |
230 |
| - "Sometimes on Windows, Python might also be accessed with:\n", |
231 |
| - "```shell\n", |
232 |
| - "py\n", |
233 |
| - "```\n", |
234 |
| - "and depending on the version of python installed, it might be accessed via \n", |
235 | 200 | "```shell\n",
|
236 |
| - "python3\n", |
| 201 | + "python --version\n", |
237 | 202 | "```\n",
|
238 |
| - "Once you have determined under which name your Python installation is under you can continue to make use of that name for the rest of the steps. \n", |
239 |
| - "\n", |
240 |
| - "You can then verify that your Python installation is working properly by running: \n", |
241 |
| - "\n", |
242 |
| - "```python\n", |
243 |
| - "x = 3 \n", |
244 |
| - "print(x)\n", |
| 203 | + "This should produce a output stating the installed python version, for example:\n", |
| 204 | + "```bash\n", |
| 205 | + "Python 3.12.4\n", |
245 | 206 | "```\n",
|
246 | 207 | "\n",
|
247 |
| - "If the output produced is `3`, then you have installed Python correctly.\n", |
| 208 | + "Minor differences in the python version (beyond the first `.`) will not matter for the most part. However, unless you have a specific reason to use Python 2, the version should start with a 3. If it does not or if Python is not found on your system then you can install it via the [official website](https://www.python.org/downloads/).\n", |
248 | 209 | "\n",
|
249 |
| - "You are able to exit the interactive session by typing: \n", |
| 210 | + "Typically Python is invoked by calling python followed by the path to a python script file, containing some code: \n", |
250 | 211 | "\n",
|
251 |
| - "```python\n", |
252 |
| - "exit()\n", |
| 212 | + "```bash\n", |
| 213 | + "python example_script.py\n", |
253 | 214 | "```\n",
|
254 | 215 | "\n",
|
255 |
| - "#### Checking Which Packages Are Currently Installed \n", |
256 |
| - "\n", |
257 |
| - "You can check which packages are currently installed by running: \n", |
| 216 | + "Calling Python from command line with no other arguments will open the [interpreter](https://docs.python.org/3/tutorial/interpreter.html) (indicated by the `>>>` prompt), where python commands can be executed in an interactive manner. This is rarely used as the code written is cleared when the interpreter is exited, which can be done by executing the `exit()` command: \n", |
258 | 217 | "\n",
|
259 |
| - "```shell\n", |
260 |
| - "pip list\n", |
| 218 | + "```bash\n", |
| 219 | + ">>> exit()\n", |
261 | 220 | "```\n",
|
262 |
| - "\n", |
263 |
| - "If you have multiple Python versions installed, you may need `pip3 list` or `py -m pip list`, depending on the particulars of your Python installation.\n", |
264 |
| - "\n", |
| 221 | + "#### Checking what is currently installed\n", |
| 222 | + "Python comes with some default packages installed already, and installation of packages is typically managed through PIP (a recursive acronym for \"pip install package\"). We can see what is currently installed system-wide using:\n", |
| 223 | + "```bash\n", |
| 224 | + "pip list \n", |
| 225 | + "```\n", |
| 226 | + "we _could_ install more packages system-wide using pip, but it is better to do so in a virtual environment.\n", |
265 | 227 | "\n",
|
266 | 228 | "#### Creating a Virtual Environment \n",
|
267 | 229 | "\n",
|
|
279 | 241 | ".\\virtual_environment_1\\Scripts\\activate\n",
|
280 | 242 | "```\n",
|
281 | 243 | "\n",
|
282 |
| - "\n", |
283 | 244 | "NOTE: Note: In PowerShell, you might need to prepend `.\\` to the command.\n",
|
284 | 245 | "Example: `.\\virtual_environment_1\\Scripts\\activate.`\n",
|
285 | 246 | "\n",
|
|
300 | 261 | "\n",
|
301 | 262 | "#### Creating Multiple Environments \n",
|
302 | 263 | "\n",
|
303 |
| - "You can create a second virtual environment, `virtual_environment_2`, as follows: \n", |
| 264 | + "You can create and activate a second virtual environment, `virtual_environment_2`, as follows: \n", |
304 | 265 | "\n",
|
305 | 266 | "```shell\n",
|
306 | 267 | "python -m venv virtual_environment_2\n",
|
|
315 | 276 | "```\n",
|
316 | 277 | "#### Installing a Package \n",
|
317 | 278 | "\n",
|
318 |
| - "Once you are inside an activated environment, you can install packages with `pip` (or `pip3` if that is what your system uses in a similar manner as before with `python`, `py` and `python3`). For example, to install [Pandas](https://pandas.pydata.org/):\n", |
| 279 | + "Once you are inside an activated environment, you can install packages with `pip` (or `pip3` if that is what your system uses in a similar manner as before with `python3`). For example, to install [Pandas](https://pandas.pydata.org/):\n", |
319 | 280 | "\n",
|
320 | 281 | "```shell\n",
|
321 | 282 | "pip install pandas\n",
|
|
327 | 288 | "```\n",
|
328 | 289 | "you should see pandas listed among your installed packages. This means you can use `import pandas` with Python scripts in this environment. If you leave the environment by typing `deactivate` and then check `pip list` again (outside the environment), you will not see pandas listed.\n",
|
329 | 290 | "\n",
|
330 |
| - "This behavior is the main advantage of virtual environments: they let you maintain different sets (and versions) of Python libraries for different projects, all on the same machine, without interference. For instance, you might need Package X that only works with Package Y version 1 for one project, while another project needs the newer Package Y version 2. With virtual environments, you can install each combination of packages only once and quickly switch between them.\n", |
| 291 | + "This is the key goal of virtual environments: allowing you to easily swap between different configurations of packages and their respective versions without uninstalling and reinstalling everything. For example it might be the case that on one of your projects you need to use `pandas 2.2.2`, but that this is incompatible with some other package used in a different project, which only works with `pandas 1.8.0`. Using virtual environments allows each project to install the working version of pandas without replacing the version installed in the other project. \n", |
331 | 292 | "\n",
|
332 | 293 | "## Python Environment and Dependency Management Tools \n",
|
333 | 294 | "\n",
|
334 | 295 | "There are a number of different options available for environmental and dependency management tools. Below are some of the most widely used tools for this task within Python. The following sections act as a set of signposts to the more comprehensive guides to their use on their official websites. \n",
|
335 | 296 | "\n",
|
336 | 297 | "### venv\n",
|
| 298 | + "(covered above)\n", |
337 | 299 | "\n",
|
338 | 300 | "[venv Tutorial](https://docs.python.org/3/tutorial/venv.html)\n",
|
339 | 301 | "\n",
|
|
463 | 425 | "```\n",
|
464 | 426 | "\n",
|
465 | 427 | "### Poetry\n",
|
| 428 | + "Poetry bundles multiple tools for building and organising projects and managing dependencies. It makes use of venv to manage dependencies through a user-friendly interface, and also incorporates a build system to make your own projects easily installable (for example with pip). This is agreat way to standardise your workflow and make your work easy to reproduce! \n", |
466 | 429 | "\n",
|
467 | 430 | "[Poetry Tutorial](https://python-poetry.org/docs/)\n",
|
468 | 431 | "\n",
|
|
471 | 434 | "```\n",
|
472 | 435 | "\n"
|
473 | 436 | ]
|
474 |
| - }, |
475 |
| - { |
476 |
| - "cell_type": "code", |
477 |
| - "execution_count": null, |
478 |
| - "id": "301c5b93-8c6e-4b23-ac74-92e2c9f9a2a8", |
479 |
| - "metadata": {}, |
480 |
| - "outputs": [], |
481 |
| - "source": [] |
482 | 437 | }
|
483 | 438 | ],
|
484 | 439 | "metadata": {
|
|
0 commit comments