# Environment Setup 25S ## Python The programming language used in this course is Python, with the version 3.12. This guide shows the steps for installing Python 3.12 for Windows, macOS and Debian Linux. The screenshots are outdated, but it is the same for the new version. <details> <summary> Windows </summary> Download the installer here: * [Windows (64-bit)](https://www.python.org/ftp/python/3.12.6/python-3.12.6-amd64.exe) 1. Open the downloaded Python installer. **Make sure to check the "Add Python 3.12 to PATH" mark!** <img src="https://i.imgur.com/qfydlwD.png" alt="Example Image" style="border-radius: 10px; box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3);"> 2. Click on "Install Now". This may take a while. <img src="https://i.imgur.com/Osksm4e.png" alt="Example Image" style="border-radius: 10px; box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3);"> 3. To finish the installation, click "Close". <img src="https://i.imgur.com/pViS2Ix.png" alt="Example Image" style="border-radius: 10px; box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3);"> ### Verify Installation To check if you successfully installed Python open up a new Terminal. Press the Windows key<kbd>![](http://i.stack.imgur.com/T0oPO.png)</kbd>, type `cmd` and hit enter. A new terminal window opens up which should look something like this: <img src="https://i.imgur.com/TJl9DoU.png" alt="Example Image" style="border-radius: 10px; box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3);"> Next, enter ``python --version`` in the terminal and hit enter. <img src="https://i.imgur.com/A3fHfcS.png" alt="Example Image" style="border-radius: 10px; box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3);"> You should see the currently installed version of Python. Any Python version 3.12.x is also fine. </details> <details> <summary> macOS </summary> You can install python using **either** the installer package <span style="color:red">**OR**</span> homebrew. #### Using the installer package (.pkg) Download the installer here * [macOS 11](https://www.python.org/ftp/python/3.12.6/python-3.12.6-macos11.pkg) 1. Open the downloaded installer package, click on "Continue" and accept the licence ![](https://i.imgur.com/N3Jl0Xt.png) 2. Click on "Install" ![](https://i.imgur.com/61IlQ5m.png) 3. Finish the installation ![](https://i.imgur.com/zssbEvy.png) 4. Open Terminal and run the command >``/Applications/Python\ 3.12/Install\ Certificates.command`` #### Using homebrew [Homebrew](https://brew.sh) is a free software package manager that can be installed by pasting the following line into the terminal (To open a terminal, press <kbd>&#8984;</kbd>+<kbd>Space</kbd>, type `Terminal` and hit enter) >``/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"`` You might need to install Xcode command line tools first, by typing the following line into your terminal >``xcode-select --install`` You can install python 3.12 by typing >``brew update && brew install python@3.12`` **Optional**: You might want to add symbolik links like python->python3 and pip->pip3 to your Path by typing > ``echo -n 'export PATH=/opt/homebrew/opt/python@3.10/libexec/bin:$PATH' >> ~/.zshrc`` ### Verify installation Open a terminal (To open a terminal, press <kbd>&#8984;</kbd>+<kbd>Space</kbd> and type `Terminal`), type `python3 --version` and hit enter. ![](https://i.imgur.com/sorMa71.png) Any Python version 3.12.x is also fine. </details> <details> <summary> Debian </summary> If you are using a Linux-distribution based on Debian (like Ubuntu), type the following lines into a terminal (The keyboard sequence to open a terminal in Linux usually is <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>T</kbd>): ```bash sudo apt update && sudo apt upgrade -y sudo apt install software-properties-common -y sudo add-apt-repository ppa:deadsnakes/ppa sudo apt install python3.12 ``` ### Verify python installation ```bash python3.12 --version 3.12.6 ``` Any Python version 3.12.x is also fine. </details> ## Poetry [poetry](https://python-poetry.org/docs/#installation) is a tool for managing virtual environments, installing packages, and building package distributions. Install it using the official script: - Linux, MacOS, WSL curl -sSL https://install.python-poetry.org | python3 - **Note:** If you encounter the error `[SSL: CERTIFICATE_VERIFY_FAILED]` on a Mac, please check if Step 4 under the Python->MacOS->"Using the installer package (.pkg)" section was executed. - Windows (Powershell) (Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | py - **Note:** If you have installed Python through the Microsoft Store, replace 'py' with 'python' in the command above: (Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | python - Check if the installation worked: poetry -V Optional (if poetry can not be found): Add poetry to PATH: <details> <summary>Linux / macOS</summary> macOS/Linux: Which shell do I have? echo $SHELL For **bash** echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc For **zsh** echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.zshrc Restart your terminal! </details> <details> <summary>Windows</summary> To add the directory containing `poetry` to the PATH: - Press the Windows key and search for **path**. - In the **System Properties** window, click on **Environment Variables...** (right lower corner). - Then, in the upper window (User variables), choose the **Path** variable and click **Edit..**. - Create a **New** Variable with: `%APPDATA%\Python\Scripts`. - Restart your terminal! After setting the path variable the `poetry install` and `poetry shell` commands should work properly. If you get the following error message after typing `poetry shell` ...\pypoetry\Cache\virtualenvs\ecg-n-ZS_Q7z-py3.10\Scripts\activate.ps1 cannot be loaded because running scripts is disabled on this system. You need to change the execution policy for your machine. This can be done with the following command: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine After executing this command, enter 'Y' in the terminal to actually change the policy. </details> ## Visual Studio Code While you can use any text editor or IDE to write your python code, we encourage you to use [Visual Studio Code](https://code.visualstudio.com/). It provides syntax highlighting, useful keyboard shorcuts, debugging capabilities, and support for the tools you will use during this course. ### Recommended VS Code Extensions - [Python](https://marketplace.visualstudio.com/items?itemName=ms-python.python) (by Microsoft) - [Ruff](https://marketplace.visualstudio.com/items?itemName=charliermarsh.ruff) (by Charlie Marsh) - [Error Lens](https://marketplace.visualstudio.com/items?itemName=usernamehw.errorlens) (by Alexander) - [Black Formatter](https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter) (by Microsoft) - [Coverage Gutters](https://marketplace.visualstudio.com/items?itemName=ryanluker.vscode-coverage-gutters) (by ryanluker) - [Even Better TOML](https://marketplace.visualstudio.com/items?itemName=tamasfe.even-better-toml) (by tamasfe) ### Recommended VS Code Settings - Files: Auto Save -> onFocusChange - Python > Analysis: Type Checking Mode -> basic - Jupyter: Run Startup Commands -> `["%reload_ext autoreload", "%autoreload 2"]` ### Set environment of VS Code to the same as the poetry environment This is sometimes necessary to remove some weird underlined code or errors that appear in VS Code. After starting VS Code and typing `poetry shell` to enter the poetry environment you can press Ctrl + Shift + P in VS Code and select "Python: Select interpreter" and then select your environment (usually marked with "poetry" on the right side).