
(4.2-setup-vscode)=
# 🧩 4.2 Setting Up VS Code

```{contents}
:depth: 3
```

## 🔰 Tutorial

In this module, you will learn how to set up VS Code and optimize it for Python development using tools like Miniconda, various extensions, and advanced features.

1. Set up VS Code
2. Install Miniconda for environment management
3. Install key VS Code extensions for Python development
4. Configure SSH for remote development
5. Explore advanced tools such as Black formatter, Pylance, and GitHub Copilot Chat

### Setting Up VS Code

First, you will download and install Visual Studio Code (VS Code), a lightweight and powerful editor. Then, you'll learn how to configure it for efficient Python development.

#### Steps:

1. Download and install VS Code from the official site: [VS Code Download](https://code.visualstudio.com/).
2. Open VS Code and get familiar with the interface, including the command palette, sidebar, and settings menu.

**Video Tutorial**: [Getting Started with VS Code](https://www.youtube.com/watch?v=VqCgcpAypFQ)

### Installing Miniconda

Miniconda is a minimal installer for Conda, which is an open-source package management system and environment management system.

1. Download Miniconda from the [official site](https://docs.conda.io/en/latest/miniconda.html).
2. Install Miniconda by following the instructions for your operating system.
3. Verify the installation by opening a new terminal window and running:
   ```
   conda --version
   ```
4. After installation, create a new Python environment:
   ```bash
   conda create -n myenv python=3.11
   conda activate myenv
   ```

5. Ensure that VS Code is set up to use this environment for Python development.

For detailed instructions on using Conda environments with VS Code, refer to the [official VS Code documentation on Python environments](https://code.visualstudio.com/docs/python/environments).

**Video Tutorial**: [How to Install Miniconda](https://www.youtube.com/watch?v=oHHbsMfyNR4&pp=ygUYSG93IHRvIEluc3RhbGwgTWluaWNvbmRh)

```{note}

Consider using [Mamba](https://github.com/mamba-org/mamba), a widely used, fast (written in C++), drop-in replacement for conda.

```

### Installing Essential VS Code Extensions

Extensions can enhance the functionality of VS Code, especially for Python development. Below are some essential extensions to install:

- [Python Extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python): Adds Python language support.
- [Pylance](https://marketplace.visualstudio.com/items?itemName=ms-python.vscode-pylance): A high-performance language server with type checking and autocompletion.
- [Black Formatter](https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter): Automatically formats your Python code.
- [autoDocstring](https://marketplace.visualstudio.com/items?itemName=njpwerner.autodocstring): Generates docstrings for your Python functions and methods.
- [GitHub Copilot](https://marketplace.visualstudio.com/items?itemName=GitHub.copilot): An AI-powered coding assistant.
- [GitHub Copilot Chat](https://marketplace.visualstudio.com/items?itemName=GitHub.copilot-chat): AI chat features powered by Copilot.
- [Remote - SSH](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-ssh): For remote development using SSH.

To install these extensions:
1. Open the **Extensions** view in VS Code (`Ctrl + Shift + X`).
2. Search for each extension by name and click **Install**.

**Video Tutorial**: [Best VS Code Extensions for Python](https://www.youtube.com/watch?v=fj2tuTIcUys&pp=ygUiQmVzdCBWUyBDb2RlIEV4dGVuc2lvbnMgZm9yIFB5dGhvbg%3D%3D)

### Using SSH for Remote Development

VS Code supports remote development, allowing you to write and run code on a remote machine as if it were local.

Usecase: Raspberry Pi 5 ssh with VS Code extension

This setup allows you to develop directly on your Raspberry Pi 5 from your main computer, combining the power of VS Code with the versatility of the Raspberry Pi.

Steps:
1. Ensure your Raspberry Pi 5 is set up and connected to your network.
2. Install the "Remote - SSH" extension in VS Code.
3. Open the Command Palette (Ctrl+Shift+P) and select "Remote-SSH: Connect to Host..."
4. Enter the SSH connection string: `pi@raspberrypi.local` (or your Pi's IP address)
5. Select the platform of the remote host (Linux)
6. Enter your password when prompted

Once connected, you can create a new Python file on the Pi and start coding. Here's an example:

```python
# File: /home/pi/hello_pi.py

import RPi.GPIO as GPIO
import time

# Set up GPIO mode
GPIO.setmode(GPIO.BCM)

# Set up pin 18 as an output
GPIO.setup(18, GPIO.OUT)

# Blink LED connected to pin 18
try:
    while True:
        GPIO.output(18, GPIO.HIGH)  # Turn on
        time.sleep(1)
        GPIO.output(18, GPIO.LOW)  # Turn off
        time.sleep(1)
except KeyboardInterrupt:
    GPIO.cleanup()  # Clean up on exit
```

This script will blink an LED connected to GPIO pin 18 on your Raspberry Pi 5. You can edit, run, and debug this code directly from VS Code on your main computer.


**Video Tutorial**: [VS Code Remote Development with SSH](https://www.youtube.com/watch?v=miyD4c1dnTU&pp=ygUjVlMgQ29kZSBSZW1vdGUgRGV2ZWxvcG1lbnQgd2l0aCBTU0g%3D)

### Configuring Black Formatter

Black is an opinionated Python code formatter that ensures code is formatted consistently. Here’s how to configure it in VS Code:

1. Open the VS Code **Settings** (`Ctrl + ,`).
2. Search for `Python Formatting Provider` and set it to `Black`.
3. You can also configure VS Code to format your code on save by enabling the "Format on Save" option in settings.

**Video Tutorial**: [How to Use Black in VS Code](https://www.youtube.com/watch?v=esZLCuWs_2Y)

### GitHub Copilot Chat

GitHub Copilot can assist you by suggesting code snippets, entire functions, and even refactoring your code. With **GitHub Copilot Chat**, you can interact with Copilot via a chat interface to get suggestions and resolve issues.

#### Steps:

1. Install **GitHub Copilot** and **GitHub Copilot Chat** extensions.
2. After installation, sign in to your GitHub account in VS Code.
3. Open the Copilot Chat interface and ask for help with coding, debugging, or writing functions.

**Video Tutorial**: [GitHub Copilot Chat](https://www.youtube.com/watch?v=a2DDYMEPwbE&pp=ygUTR2l0SHViIENvcGlsb3QgQ2hhdA%3D%3D)

### Additional Resources

- [VS Code Documentation](https://code.visualstudio.com/docs)
- [Conda Environments Guide](https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html)
- [Setting up VS Code for Python](https://code.visualstudio.com/docs/python/python-tutorial)
- [Black Formatter Documentation](https://black.readthedocs.io/en/stable/)
- [GitHub Copilot Documentation](https://docs.github.com/en/copilot)

## 🚀 Quiz

::::{tab-set}
:sync-group: category

:::{tab-item} W 2024
:sync: w2024

:::

:::{tab-item} Sp/Su 2024
:sync: sp2024

https://q.utoronto.ca/courses/370070/assignments/1393623?display=full_width
:::

:::{tab-item} Sp/Su 2025
:sync: sp2025

https://q.utoronto.ca/courses/393352/assignments/1499723?display=full_width_with_nav
:::

::::

## 📄 Assignment

::::{tab-set}
:sync-group: category

:::{tab-item} W 2024
:sync: w2024

:::

:::{tab-item} Sp/Su 2024
:sync: sp2024

https://q.utoronto.ca/courses/370070/assignments/1394226?display=full_width
:::

:::{tab-item} Sp/Su 2025
:sync: sp2025

https://q.utoronto.ca/courses/393352/assignments/1499724?display=full_width_with_nav
:::

::::
