Remote Python coding with VSCode on the Raspberry Pi | Agile Partner
share on

Remote Python coding with VSCode on the Raspberry Pi

By Olivier Robert, a Senior Consultant and DevOps Engineer at Agile Partner.

Like a lot of people out there, I like to tinker with the Raspberry Pi. Until now, I used it mailny as a small linux host to run services, as a media server, as a NextCloud server, … You know, the usual suspects. But lately, I started to play with electronics. I bought a breadboard and a discovery pack to get going. Obviously, that means coding tiny programs in C or Python. Python was my choice.

I always install my RPi’s headless. I don’t need the overhead of a graphical interface and feel at home in a shell. Installing VSCode is going to be difficult. Right! More on that later. My editor of choice is Vim. I’m far from being a guru at it, but I get things done pretty easily for my needs. A few years ago, I had used vundle and a long list of plugins painstakingly configured and customised to make Vim a real power house. That took time, research, trial and errors, … I wasn’t ready to invest the time for that, I wanted to start playing with little circuits.

So naked Vim it is. One script later and missing auto completion, I decided to look for a better solution: SpaceVim. That’s even better than my long gone power house Vim setup. I very quickly found how to configure it as a Python IDE. I was relieved, this was working pretty nice. A bit sluggish, but usable.

Then a colleague sent me a link to Remote Development: a VSCode extension pack that looked interesting. He thought the remote-container extension would get my blood flowing. Rightly so. But the remote-ssh extension triggered my interest out of immediate necessity. I have no use for the 3rd extension for WSL (the Windows Subsystem for Linux). I don’t use WSL, I work on a Mac. But I am sure Windows users will love it. I did not install the pack, but remote-ssh and remote-container are now part of my favorite extensions.

I suggest to get VSCode Insiders as the extensions are still in preview as of this writing.

I installed the remote-ssh extension and my ssh config server names showed up in the VSCode connection menu. I just needed to pick and chose. Here is a sample of my ssh config file.

Host *
  AddKeysToAgent yes
  ServerAliveInterval 240
 
[...]
 
Host pi
  HostName 192.168.2.137
  User pi
  IdentityFile ~/.ssh/id_rsa
 
Host nfs
  HostName 192.168.2.91
  User bob
  IdentityFile ~/.ssh/id_rsa
 
Host git-codecommit.*.amazonaws.com
  User AEDHIBTP4LZ6SDU4V2IQ
  IdentityFile ~/.ssh/id_rsa
 
[...]

If you need more details, it’s all very well explained here.

The connection gets established. On the remote host, you’ll find the .vscode-server-insiders folder in your home directory on the remote host as expected.

pi@raspberrypi:~/.vscode-server-insiders $ l
 bin
 data
 extensions

VSCode shows no extensions on the remote host. The extensions window is now split to show local and remote extensions. I can just browse locally installed extensions and with the click of the mouse, they are installed on the remote host. Very nice.

Time to test it. I’ll create a python virtual environment, select the python interpreter, open a python file, …, yes, install pylint for me please, then I install RPi.GPIO,…, we should be good to go.

And I get this: “Operating system does not meet the minimum requirements of the language server. Reverting to alternative, Jedi”. Hummm!

And I see a problem in the VSCode Problems tab. The linter does not recognize the imported lib and all GPIO methods are shown as not existing.

Auto completion works perfectly though.

I decided to disable Jedi and so far so good. I do not know if I just get a speed penalty for using the Microsoft Python Analysis Engine. But if I do, it ok. It’s a Raspberry Pi, I’m gonna be fine.

FWIW, I tested another Python project on a Centos 7 virtual machine and it worked without any issues. So, fingers crossed this is just a speed thing.

Added bonus, when remote connected to a host, the VSCode shell is not the local shell any more, it is, as expected the remote shell. So you get to use all the powerful features of the editor and you can use the embedded shell for all your cli needs.

The remote-ssh extension is great and can be useful in many ways:

So, sure, it is super convenient, but what is the deal with the Raspberry Pi? Is it just to bait you because the Pi has that cool/fun factor?

No! Try to install the RPi.GPIO module on your Mac, or Windows host. It fails! Google around and you’ll see a few people wanted to code from the comfort of their main machine. Well, in this particular case, you can’t. You need to develop on the Pi itself. There is even a fake-rpi library (that I have not tested) to avoid having to develop on the Pi. I can use the real deal from the comfort of my Mac and VSCode, having all my customisations, drop down to the shell if needed, execute my program and test the results on my breadboard. Priceless!

Try it out!

Want to know more? The experts of our Agile Software Factory are here to help you!

share on