Visual Studio Code for programming a Raspberry Pi

Visual Cues

© Lead Image © lightwise, 123RF.com

© Lead Image © lightwise, 123RF.com

Article from Issue 243/2021
Author(s):

Professional and casual Python developers save time and keystrokes in a Visual Studio Code development environment for remote coding of a Raspberry Pi.

Writing software code is heavy work, and professional code writers leverage software tools to create an effective environment that allows developers to work on code without affecting the users or breaking anything in the live environment.

Development environments are typically set up on a local machine or server that reflects the target production environment and contains the code being developed or modified. An integrated development environment (IDE) is a software application programming tool, deployed in a development environment to assist developers in maximizing their productivity and efficiency.

The IDE needs to be compatible with the programming language of interest and typically contains source code editors, a debugger, a compiler, and designers that can all be accessed through a single interface. Combining all of the development tools into one software suite helps the coder work on multiple tasks through one interface.

The repetitive commands used to write, run, and change code can be optimized in an IDE. Microsoft Visual Studio Code (VS Code) [1] is a tool for professional code developers (but see the "IDEs for All" box) that is usually used on standard Windows, Linux, and macOS machines; however, it can also be deployed to help make the life of a Raspberry Pi programmer easier.

IDEs for All

If you don't consider yourself a professional coder, why invest in an IDE? Maybe you're a code resurrectionist, or Frankenstein programmer.

Resurrectionists were paid body snatchers who exhumed the bodies of the recently dead for anatomists in the United Kingdom during the 18th and 19th centuries. In Mary Shelley's classic horror novel Frankenstein; or, The Modern Prometheus, Dr. Frankenstein stitches together an assortment of stolen body parts and attempts to imbue life into his creation.

Likewise, code resurrectionists look through Python code to find pieces that they can bring back to life by combining them with other pieces of code to make something useful. The cycle is similar to the instructions "wash, rinse, and repeat" found on a shampoo bottle: Find code snippets, combine them, modify them, write code when desperate, deploy the mix, and test. An IDE is the perfect tool for this programming cycle.

Environmental Decisions

VS Code is a streamlined code editor with support for development operations like debugging, task running, and version control. Although the Raspberry Pi has developed through the years into a fairly powerful single-board computer, it is probably not appropriate to run a full development environment and run the Python application being developed all on the same Rasp Pi

VS Code is an editor first and foremost. Instead of installing it on a Raspberry Pi with a keyboard, mouse, and video screen attached [2], you can deploy the VS Code tools for remote debugging. For this exercise, I installed VS Code on a workstation running Ubuntu 20.04; then, VS Code installed its headless server component designed for the ARM7 processor on the Raspberry Pi (Figure 1). This installation setup allows you to write and modify code from a local workstation and run and test the code on a Raspberry Pi (see the "Materials" box).

Figure 1: Instead of installing VS Code on the Raspberry Pi, I opted for a workstation installation with a connection to the Pi.

Materials

  • Workstation: 64-bit Ubuntu 20.04.1 LTS
  • Visual Studio Code: version 1.47.3
  • Pi3B+, 8GB SD card
  • Raspberry Pi OS (previously called Raspbian) 2020-05-27-raspios-buster-lite

Preparation

The first exercise in creating a development environment with VS Code is to establish an SSH connection from the Ubuntu workstation to the Raspberry Pi and configure it so that no password is required.

To generate a public and private public key infrastructure (PKI) key pair and transfer the public key to the Raspberry Pi host (Figure 2), issue the following commands from a command-line interface (CLI) on the workstation:

ssh-keygen <optional passphrase>
ssh-copy-id -i ~/.ssh/id_rsa.pub pi@<Pi IP Address>
Figure 2: Output from an SSH key generation command, retaining default file names.

Being security conscious, best practice is to include a passphrase when prompted. You should retain the file defaults, unless you have the knowledge to customize the configuration.

The command

ssh-copy-id -i ~/.ssh/<mykey> <user>@<host>

logs in to the Raspberry Pi, copies the public key file, and configures the key in the authorized_keys file to grant access. The copying may ask for a password or other authentication to connect with the Pi.

After the SSH configuration, the first time you log in requires you to supply a password. After that, SSH connections should be made without password prompting. You will know you are successful when you enter

ssh pi@<Pi IP Address>

and it drops you into a remote prompt without the need for a password.

Now that you have a Raspberry Pi SSH connection working without password prompting, you need to tailor the Pi for a development environment. Some additional software needs to be added so that it is prepared to support the changes that VS Code is going to make to establish a remote connection to the Pi. The following commands entered from the CLI on the Raspberry Pi adds the needed software:

sudo apt update
sudo apt upgrade
sudo apt install git python-pip python3-pip

The update and upgrade commands should always precede any Raspberry Pi software install requests. The commands refresh the environment and prepare it for the new installs.

The apt install command loads the Git distributed version control system and pip, the package manager for Python packages for the Python 2 and 3 environments. Packages contain the files needed for a module, which is a Python code library. If VS Code wants to install Python software, it is going to call pip to make it happen.

Host Workstation Installation

On the Ubuntu workstation, you should install VS Code from the Ubuntu Software Center. For Ubuntu 20.04, the Software Center will list two versions of VS Code: Visual Studio Code and Visual Studio Code Insiders. The description provided in the window for both are identical, so what is the difference?

Visual Studio Code is the stable version, whereas Visual Studio Code Insiders is the daily release with the most recent code pushes. The Insider builds have new features, bug fixes, and other recently closed issues. If you like working on the edge of software development, consider a side-by-side install (i.e., Insiders installed next to the stable build), so you can use either version independently.

If using the latest releases of VS Code appeals to you, you might want to consider removing the Remote-SSH extension and replacing it with the Remote-SSH (Nightly) extension. The Remote-SSH extension lets you use any remote machine with an SSH server as your development environment. The nightly build version of the extension provides the developer early feedback and testing. This extension works best with VS Code Insiders. You must uninstall the stable version of the extension before using the nightly version. Be aware that bleeding edge builds may lead to the occasional broken build.

After firing up VS Code, you will see a welcome screen. Typically, I would start by exploring all the options of the VS Code interface, but I will not use that approach for the moment and pick it up later when VS Code is connected to the Raspberry Pi. For now, I will consider how to configure the application environment for Python.

Some changes are needed in the VS Code installation before proceeding. To begin, at the bottom of the left-hand vertical Activity Bar, select Extensions (the Tetris, or falling block, icon). Extensions add features to VS Code, and you can install those you need or uninstall those you don't require. Recall that the goal is to create a development environment for Python coding on the Raspberry Pi.

In the Extensions Search field, search for extensions that have "python" in the name, and click the Install button for ms-python.python to install the Microsoft Python extension (Figure 3). For a development environment, you should add support for Python code formatting (discussed in greater detail later) by installing the Python-autopep8 (himanoa.python-autopep8) extension.

Figure 3: The VS Code extension trifecta for Python support.

Recall that you did some configuration work earlier on the Raspberry Pi to enable SSH, so you should add that feature to VS Code by installing the Remote-SSH (ms-vscode-remote-ssh) extension.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Raspberry Pi WebIDE
  • PyPy and Nuitka

    PyPy and Nuitka improve the performance of Python on a Raspberry Pi.

  • GitHub Codespaces

    Imagine you want to build a program from the source code and discover that your distribution lacks the tool and software package versions you need to do so. Instead of using your own virtual machine, you can now switch to GitHub Codespaces.

  • influZend Studio 4

    PHP experts consider Zend Studio the most mature and feature-rich IDE for PHP. The latest version offers enhanced database manipulation and other improvements. We’ll put the Zend Studio IDE through its paces and show you how you can use it to speed up PHP development.

  • CircuitPython

    The CircuitPython run-time environment runs on almost all microcomputers and microcontrollers, making it perfect for cross-platform programming.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News