Remote Python Debugging with VSCode

 last updated 2020.08.20

In VSCode, remote debugging is possible with id/password method without generating ssh key. See VSCode Remote Python Debugging without ssh keys.

 If you are Korean, there's a same article written in Korean(한글) at https://github.com/raspberry-pi-maker/RaspberryPi-For-Makers/blob/master/tips/chap-01/vscode.md

Most programming tasks on Linux systems use remote tasks using SSH terminal. Some developers passionately likes the vi editor but beginners who are not good at vi command want to use GUI IDE(Integrated Development Environment) like Microsoft Visual Studio.
In this article, I'll show you how to set up remote development environment for Python. Remote system may be Raspberry Pi, Jetson Nano or X64 Linux systems. So you can apply this method to most systems.
Because the process runs on a remote target machine, you do not need to install any source code or development tools on your local machine. In addition, despite the differences in operating systems, it is possible to debug Python programs to run on remote machines in various OS(Linux, Windows, and Mac) regardless of your OS.



Install VSCode Insider

To use VSCode extensions for remote development using SBC, such as Raspberry Pi, Jetson Nano, and Odroid, which currently use Arm CPUs (October 2019), the VSCode Insider must be installed first. VSCode Insider is almost identical to VSCode, but gives you the opportunity to introduce and test the latest features first. VSCode also provides Remote Development extensions, but does not yet support the Arm family of CPUs. However, if the stability is verified in VSCode Insider, it will be applied to VSCode, so always refer to the page provided when installing the extension. Download and install VSCode Insider from https://code.visualstudio.com/insiders/. Mac and Linux versions are also available, so download them according to your development environment.


Install VSCode Remote development extension

Run the VSCode Insider (hereafter VSCode) and press the Extensions button. Then query for remote development. The query will look like the following figure.

remote-development

You can see that the Preview mark is on the upper right corner at the present time(2019.10). If you read the description, you can see that installing this extension installs three extensions: Remote-SSH, Remote-Container, and Remote-WSL.


Install VSCode Python extension

Next, search for and install the Python extension.

python


Setup Remote SSH connection

My remote development machine is Raspberry Pi. Check your remote machine's IP and then proceed. In this article, I will use a pie with the value 192.168.11.89.


SSH Key generation on your desktop

To connect to a remote device (Raspberry Pi) using the VSCode Remote Development extension, you must use the ssh key. First, generate a key through the following steps. This step makes two keys. One is a private key and the other is a public key. Open a console (DOS terminal) and generate a key with the following command: The question of the creation process can be passed to Enter.

keygen

Copy Public Key to remote machine

You can see that the generated keys are in the C: \ Users \ dongu / .ssh / directory. Now it's time to copy the public key to the remote computer (Raspberry Pi). First use the scp command on your PC to copy the key into the pie: You can also use a tool like FileZilla. Copy the public key (id_rsa.pub) into the pie's root home directory. (I'm currently working with the root account. If you want to work with the pi account, change root to pi in the command below.) Information on remote root access can be easily found in the internet.


PS C:\Users\spypiggy> scp "C:\Users\dongu/.ssh/id_rsa.pub" root@192.168.11.89:~/tmp.pub


Set the SSH Public Key

After connecting  to the remote machine, proceed as follows. Allow access through the key you just created and restart the ssh service. Note that you must work with the same root account so that your home path (~) matches.

command

You can now access your remote machine using your private key without id / pw.


Setup VSCode

Setup VSCode Remote Development Configuration

Now go back to your PC, run VSCode and press Ctrl + Shift + P to search for the extension command. Then select Remote-SSH: Connect to Host ... Then select C: \ Users \ Users.ssh \ config. And add the following line: Modify the IP, User, and Private Key routes as appropriate.

config


Press Ctrl + Shfit + P again to see the Raspberry item you just created. Select this item to connect to the pie.
config2

If there is no problem, a new VSCode window will appear as shown in the figure below and a green connection will be displayed at the bottom left.
ide


Open Remote Folder

If you press the Explorer button and then the Open Folder button, you can specify the working directory as shown below.
dir

Now you will see a list of directory files on the remote machine on the left, with an ssh terminal on the bottom: Click on the file you want to edit.

The work thus far has shown tremendous productivity gains. You can edit the file in the GUI and run it directly in the terminal below to see the results. Finally, let's add a remote Python debugging feature to create a powerful debugging environment.

Remote Debugging

Install Remote Python Extension on the Remote machine

Remote debugging requires that you install an extension that enables remote debugging on the remote computer . Press the Extensions button or Ctrl + Shift + X and search Python again. Earlier we installed the Python module. This time we see a new Install in SSH: RaspberyPi button. Clicking this button installs a remote extension for Python debugging on the Raspberry Pi.
ide3

After installation, reload the VSCode workspace. You are now ready for Python remote debugging.

Code completion with IntelliCode

Most users who are familiar with IDE environments such as MS Visual Studio love to use code completion features. Code auto-completion not only speeds up development, but also greatly improves productivity by dramatically reducing the amount of typographical errors. Although the Linux development environment has some of these code autocompletion features, unlike the IDE on Windows and Mac, they often have limited functionality or require a paid product. In particular, remote development environments rarely provide this functionality. Using a remote control tool such as VNC is far from the remote development environment described in this article.


After installing the remote Python debugging module, VSCode can now write Python code that works on the remote computer using autocomplete. It is a new and powerful feature that you will never experience using the VI editor on a SSH terminal.

Test Python Remote Debugging

You can now set breakpoints by hovering your mouse next to the left number in the Python source code. Create a breakpoint at the desired location.
ide4

Then press the debugger button and then the start debugger button. When asked what to debug, select Python File. This will automatically run the Python file in the current window.

The program automatically stops at the breakpoint you specify. At this point, you can check the variable values ​​with the mouse and monitor the variable values ​​step by step using the left window.



ide6





Wrapping up

At this point, C / C ++ extensions do not support ARM CPU, so if you are using the C / C ++ language on the Jetson series, Raspberry Pi, Odroid, you will not be able to debug. However, I hope that this problem will be solved soon. The Python remote debugging described above can be used not only on Raspberry Pi, but also on X64 Linux servers and SBCs using various ARM CPUs. I hope this helps you improve your productivity.


댓글

이 블로그의 인기 게시물

VSCode - Lua programming and debugging

Remote C/C++ Debugging with VSCode #1 - Installation and simple debugging