[PYTHON] Edit and debug the code in the Raspberry Pi with VS Code's SSH connection feature

What is VS Code?

Abbreviation for "Visual Studio Code", a source code editor developed by Microsoft. It is said that it is rapidly gaining popularity as a free, lightweight, and multifunctional editor.

Since the 2019/10 version, the SSH connection function to RaspberryPi (ARM v8) is officially supported, so I wrote an article about the result of trying Python code remote editing in Raspberry Pi.

** In conclusion, we were able to realize a comfortable remote development environment, such as debugging with breakpoints! ** **

Things necessary

・ RaspberryPi (RaspberryPi3 Model B in this example) -PC connected to the same network as above (Windows 10 in this example) -Visual Studio Code 1.39.2 or later (installed on the above PC, 1.44.2 is used in this example)

procedure

I referred to the following article https://qiita.com/hukatama024e/items/9f96730381f0a34c6da6

(1) SSH connection settings and saving of config file

[This article "For Public Key Authentication"](https://qiita.com/c60evaporator/items/2384416f1122ae124f50#%E5%85%AC%E9%96%8B%E9%8D%B5%E8%AA%8D % E8% A8% BC% E3% 81% AE% E5% A0% B4% E5% 90% 88% E6% 89% 8B% E9% A0% 86) to establish SSH public key authentication and config file Please save.

In addition, please save the config file in the following folder in the PC C: \ Users \ [username] \ .ssh

② Install VS Code

Please install VS Code on ** PC side **

Reference article https://qiita.com/psychoroid/items/7d85ae6bade4a67aedb1

③ Installation of Remote Development

Start VSCode and follow the steps below to install Remote Development remotedevelopment.png

④ Connect to Raspberry Pi

Follow the steps below to select the SSH server to connect to = RaspberryPi ("raspi" in the figure below) sshtargets.png

A new window will open and you will be asked to select a platform, so choose Linux selectplatform.png

You will be asked for a password for public key authentication, enter it and press Enter sshkey.png

The first connection takes time, so wait for a while. If successful, you can access the files in the Raspberry Pi by clicking the red frame in the figure below. home.png

⑤ Create code in Raspberry Pi

From here onward, you can use the console, but we will proceed with the description on the assumption that you will operate with the GUI.

**-Create an empty file ** Create an empty file by selecting "File"-> "New File" makenewfile.png

** ・ Description of code contents ** For example, write the following Python code (calculate and display the standard deviation with numpy) pythontestcode.png

**-Save code ** "File"-> "Save As"-> specify save path-> "OK" filesave.png savepython.png

**-Try running from the console ** Open the console by selecting "Terminal" → "New Terminal" openterminal.png Go to the folder with the code and

python3 test.py

Run python code in runconsole.png You can see that the result is output to the console normally.

⑥ Debug execution of the created code

With VS Code GUI, you can even debug the code. You can also stop at a breakpoint, so there is no doubt that development efficiency will increase!

**-Install Python linter ** If you have already installed it locally on your PC, click "install in SSH: [hostname]" to install the Python linter on your Raspberry Pi as well. installpythonlinter.png

When the installation is complete, "Reload Required" will appear, so click it to restart. reloadrequired.png

**-Open a working folder (usually the folder with the code) ** openfolder.png

**-Debugging ** After opening the code you want to debug, execute the following operation runanddebug.png

The console will open and debug will be performed debugconsole.png

** ・ Breakpoint operation check ** As shown in the figure below, you can stop processing at a breakpoint and check the variable contents. debugbreakpoint.png

Now you can remotely debug the code in your Raspberry Pi with VS Code. ** The environment for quick development with GUI is ready, and I am personally satisfied with the result! ** **

Addendum: When an error occurs when importing a library installed with pip

When building an environment with pyenv on the Raspberry Pi side, I get the following error when importing a library installed with pip.

Error details: Unable to import'bluepy' pylint (import-error) unableimport.png

Cause

As described in here, It seems that the cause is that the location of Python referenced when debugging with VS Code and Python of pyenv are different.

approach

/home/[username]/.vscode-server/extensions/ms-python.python-‥/pythonFiles/.vscode It is in settings.json Open and add the last line below

settings.json


{
    //
    //Original description
    //
    "python.pythonPath": "[Python location for pyenv]"
}

If you still get an error

Create a ".vscode" folder in the work folder opened in ⑥, Create the following settings.json file in the folder

settings.json


{
    "python.pythonPath": "[Python location for pyenv]"
}

This should force pyenv's Python to run

Recommended Posts

Edit and debug the code in the Raspberry Pi with VS Code's SSH connection feature
Edit the file of the SSH connection destination server on the server with VS Code
Cross-compiling Raspberry Pi and building a remote debugging development environment with VS Code
Debug Python with VS Code
Settings to debug the contents of the library with VS Code
It was great to edit the Python file in the Raspberry Pi with Atom's remote function
Get the strongest environment with VS Code, Remote-Containers and remote docker-daemon
Debug settings in virtual environment when using Pipenv with VS Code
Get GrovePi + sensor value with Raspberry Pi and store it in kintone
RabbitMQ message notification app in Python with Growl ~ with Raspberry Pi and Julius ~
Impressions and memorandums when working with VS code for the first time
Pet monitoring with Rekognition and Raspberry pi
Pass PYTHONPATH in 1 minute with VS Code
Find it in the procession and edit it
[January 2020] Let's start the explosive "development container" with VS Code's Remote Container in earnest.
Make a thermometer with Raspberry Pi and make it visible on the browser Part 3
I tweeted the illuminance of the room with Raspberry Pi, Arduino and optical sensor
Learn the trends of feature words in texts with Jubatus and categorize input texts
IPsec gateway VPN construction with CentOS 8 and openSUSE (Raspberry Pi) --2 StrongSwan VPN connection confirmation
Log in to the remote server with SSH
Debug with VS Code using boost python numpy
MQTT RC car with Arduino and Raspberry Pi
Observe the Geminids meteor shower with Raspberry Pi 4
Easy connection between Raspberry Pi and AWS IoT
Use Python in Anaconda environment with VS Code
Get temperature and humidity with DHT11 and Raspberry Pi
Raspberry Pi and AWS IoT connection program example
Realize a super IoT house by acquiring sensor data in the house with Raspberry Pi
Describe the multi-stage ssh destination in the config, log in easily, and copy the file with scp
[Note] How to write QR code and description in the same image with python
Simple VPN construction of IPsec gateway with Ubuntu 20.04 and Raspberry Pi --2 StrongSwan VPN connection confirmation
I tried to build an environment where work in the Docker container on the remote server can be done directly from the local VS Code with SSH connection
Record temperature and humidity with systemd on Raspberry Pi
Machine learning with Raspberry Pi 4 and Coral USB Accelerator
Easy IoT to start with Raspberry Pi and MESH
Display images taken with the Raspberry Pi camera module
Try to visualize the room with Raspberry Pi, part 1
Try touching the micro: bit with VS Code + Python
Allow real-time code checking in Python development with VS Code
VS Code settings for developing in Python with completion
Detect mask wearing status with OpenCV and Raspberry Pi
Revive symbol search in Python workspace with VS Code
Take the value of SwitchBot thermo-hygrometer with Raspberry Pi
Measure temperature and humidity with Raspberry Pi3 and visualize with Ambient
Log the value of SwitchBot thermo-hygrometer with Raspberry Pi
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Getting Started with Yocto Project with Raspberry Pi 4 and WSL2
Remote debug Django environment created with docker-compose with VS Code
Troubleshoot with installing OpenCV on Raspberry Pi and capturing
Make a note of what you want to do in the future with Raspberry Pi
Let's statically check and format the code of E2E automatic test written in Python [VS Code]