🧩 4.2 Setting Up VS Code#

🔰 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.

  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

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.

  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:

    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.

Video Tutorial: How to Install Miniconda

Note

Consider using 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:

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

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:

# 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

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

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

Additional Resources#

🚀 Quiz#

📄 Assignment#