Using VS Code for remote Python
This page describes how to use Visual Studio Code to do remote development in Python.
** UPDATED for v2.x **
Table of Contents
References
Intro
Python is the platform of choice for developing on the MPCam. While you can use many different IDEs, we like to use Visual Studio Code because a) it is available on all major OSes (Windows, MacOS, Linux), and b) it provides a wide collection of add-ons / extensions to cover most development setups.
This page describes how to use Visual Studio Code (VSC) to develop remotely in Python on the MPCam.
Prerequisites
You will need:
a networked MPCam
an OpenSSH enabled host OS
Visual Studio Code (tested with v1.61.1):
we are assuming that:
your MPCam is on the same network as your local development host.
you have already configured your MPCam for SSK Key Logging.
Overview
When developing on the MPCam, the following process is typical:
local host is used to hold the repo or copy of the MPCam project (*.py, *.json, etc…)
local host project files are first copied onto the MPCam.
VSC is then used to dynamically edit/run/debug the project on the MPCam.
when done, the edited remote project is copied back onto the local host.
The following paragraphs will cover what extensions to install and how to use them.
Note: instructions tested on Ubuntu 18.04
VSC Extensions
The following VSC extensions should be installed…
Python
This extension adds rich support for the Python language in VSC.
To install the extension:
in VSC, open the Extensions: Marketplace
search for “Python” then Install it
You’ll need to install it on each host (local & remote) where you want to run/debug python code.
Remote - SSH
This extension uses SSH to allow VSC to run/debug code on the MPCam.
To install the extension:
in VSC, open the Extensions: Marketplace
search for “Remote - SSH” then Install it
IMPORTANT: you will need an OpenSSH client on your host (see Prerequisites)
Note: The very first time that VSC connects to an MPCam, it will download and install the VSC Server.
Loading & Debugging a Project
A project starts on your local development workstation. There, you’d usually clone a git repository or start one from scratch. As an example, we’ll use the MPCam simple-demo hosted in github: GitHub - siana-systems/mpcam-simple-demo: A simple MPCam Python demo using VSC
Assuming your git client is properly configured, you should be able to clone this public repo:
git clone git@github.com:siana-systems/mpcam-simple-demo.git
Note: a VSC project should include a custom VSC workspace settings file in its root folder in ./.vscode/settings.json, which contains project-wide tunables for VSC and its extensions (note: you should commit this .vscode folder.) The siana test project already contains a .vscode folder.
Connecting to the MPCam
To work remotely on the MPCam, we first need to connect VSC to the MPCam. We’ll use VSC’s Remote-SSH to do that. IMPORTANT: you should have already configured your MPCam for SSH Key Logging.
Open VSC
click on View > Command Palette (or Ctrl + Shift + P)
search & select: “Remote-SSH: Connect Current Window to Host…”
select your mpcam host: mpcam.local
VSC will open a remote connection. The first time, VSC will also install the VSC Server on the MPCam:
On completion/success, the VSC’s lower left corner should show: “SSH: mpcam.local.” You are connected!
Working on the MPCam
Next, we’ll open a remote folder:
click on View > Explorer, or its icon (or Ctrl + Shift + E)
click on “Open Folder” and type in the target directory:
for example, we prefer to work in /data for development
At this point, to transfer folders/files back & forth between VSC and the MPCam, all you have to do is drag folders/files in & out of VSC. For example, on your workstation, open the file explorer, e.g. Nautilus, and click-hold & drag the mpcam-simple-demo folder into the VSC’s Explorer pane: it’ll then be copied (over ssh.) From then on, VSC is used to remotely edit those files directly on the MPCam.
At this point, you are ready to Run/Debug on the MPCam in much the same that you would locally.
The first time your use the VSC Server to run/debug Python code, you will be prompted to install the Python extension on the MPCam (same as you did on your local host.)
Pulling updates from the MPCam
As you modify your remote project, such as edit and add new files, you’ll want to transfer those updated files back to your local workstation. To do so, you can simply click-hold & drag files from the VSC Explorer into Nautilus.