|
| 1 | +--- |
| 2 | +kernelspec: |
| 3 | + name: python3 |
| 4 | + display_name: 'Python 3' |
| 5 | +--- |
| 6 | + |
| 7 | +# L0 Setting up the virtual environment |
| 8 | + |
| 9 | +*License: CC-BY-NC-SA 4.0* |
| 10 | + |
| 11 | +*Author: Murilo M. Marinho (murilo.marinho@manchester.ac.uk)* |
| 12 | + |
| 13 | +## Prerequisites for the learner |
| 14 | +This lesson has no prerequisites. It is designed to be the first lesson in the course. |
| 15 | + |
| 16 | +## I found an issue |
| 17 | +Thank you! Please report it at https://github.com/MarinhoLab/OpenExecutableBooksRobotics/issues |
| 18 | + |
| 19 | +# Introduction |
| 20 | + |
| 21 | +Before starting with the lessons, you need to set up a suitable Python environment. |
| 22 | +This lesson guides you through creating a Python virtual environment and installing all the dependencies required for this project. |
| 23 | + |
| 24 | +A **virtual environment** is an isolated Python environment that allows you to install packages without affecting your system-wide Python installation. This ensures reproducibility and avoids dependency conflicts. |
| 25 | + |
| 26 | +# Creating a virtual environment |
| 27 | + |
| 28 | +The recommended approach is to use Python's built-in `venv` module. Open a terminal and run: |
| 29 | + |
| 30 | +```bash |
| 31 | +python3 -m venv venv |
| 32 | +``` |
| 33 | + |
| 34 | +This creates a directory called `venv` in your current working directory containing the virtual environment. |
| 35 | + |
| 36 | +# Activating the virtual environment |
| 37 | + |
| 38 | +Before installing packages or running the notebooks, activate the virtual environment: |
| 39 | + |
| 40 | +### Linux / macOS |
| 41 | +```bash |
| 42 | +source venv/bin/activate |
| 43 | +``` |
| 44 | + |
| 45 | +### Windows (Command Prompt) |
| 46 | +```cmd |
| 47 | +venv\Scripts\activate.bat |
| 48 | +``` |
| 49 | + |
| 50 | +### Windows (PowerShell) |
| 51 | +```powershell |
| 52 | +venv\Scripts\Activate.ps1 |
| 53 | +``` |
| 54 | + |
| 55 | +Once activated, your shell prompt should display `(venv)` to indicate the virtual environment is active. |
| 56 | + |
| 57 | +# Installing the required packages |
| 58 | + |
| 59 | +All lessons in this project use the following core dependencies: |
| 60 | + |
| 61 | +- **numpy**: Numerical computing library for arrays, matrices, and linear algebra operations. |
| 62 | +- **matplotlib**: Plotting library used in later lessons for visualisation. |
| 63 | + |
| 64 | +Run the following command to install them: |
| 65 | + |
| 66 | +```bash |
| 67 | +pip install numpy matplotlib |
| 68 | +``` |
| 69 | + |
| 70 | +# Verifying the installation |
| 71 | + |
| 72 | +You can verify that the packages are correctly installed by running the cell below. |
| 73 | + |
| 74 | +````{code-cell} |
| 75 | +import numpy as np |
| 76 | +import matplotlib |
| 77 | +import matplotlib.pyplot as plt |
| 78 | +
|
| 79 | +print(f'numpy version: {np.__version__}') |
| 80 | +print(f'matplotlib version: {matplotlib.__version__}') |
| 81 | +```` |
| 82 | + |
| 83 | +# Building the documentation (optional) |
| 84 | + |
| 85 | +To build the MyST documentation locally, you will also need: |
| 86 | + |
| 87 | +```bash |
| 88 | +pip install mystmd jupyter-server ipykernel |
| 89 | +``` |
| 90 | + |
| 91 | +Then, from the repository root, run: |
| 92 | + |
| 93 | +```bash |
| 94 | +myst build --html |
| 95 | +``` |
| 96 | + |
| 97 | +This produces the HTML output in `_build/html/`. |
| 98 | + |
| 99 | +# Deactivating the virtual environment |
| 100 | + |
| 101 | +When you are finished working, you can deactivate the virtual environment: |
| 102 | + |
| 103 | +```bash |
| 104 | +deactivate |
| 105 | +``` |
| 106 | + |
| 107 | +# Summary |
| 108 | + |
| 109 | +This lesson covered: |
| 110 | + |
| 111 | +- Creating a Python virtual environment using `venv`. |
| 112 | +- Activating and deactivating the virtual environment. |
| 113 | +- Installing the required packages (`numpy` and `matplotlib`). |
| 114 | +- Verifying the installation. |
| 115 | +- Optional: Installing MyST build tools for local documentation builds. |
| 116 | + |
| 117 | +Now you are ready to proceed to [Lesson 1](./lesson1_tutorial.md). |
0 commit comments