Personal best practices for VS Code-fronted Python development environments

Overview

When doing machine learning and data analysis, it is rare that the analysis / development work is completed only with the machine at hand. I think that it is being developed using Docker etc. on a remote server with GPU Morimori.

Starting from VS Code, I made a trial and error about Python development in a remote environment, so it is a summary.

I'm a beginner just starting to use VS Code, so I would appreciate any advice.

environment

It is a working environment. I don't think I'm bound by this environment.

--local machine: Macbook Pro - VSCode: v1.48.2 --Remote OS: Ubuntu 20.04 LTS

What you want to achieve

Since it is an integrated development environment, I want to start VS Code on a local machine and complete the work with only local VS Code.

--Ssh connection from VS Code to remote server --Connect to Docker container on the destination server --Debugging while visually setting breakpoints, etc.

procedure

SSH connection to remote server

Remote Development Use the extension. This extension seems to be a collection of remote development extensions for SSH, Container, and WSL (they look the same even if they are installed).

Reference: https://code.visualstudio.com/docs/remote/ssh#_getting-started

Introduction of Extension

Remote Development:https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.vscode-remote-extensionpack

Add remote server

Open the command palette and select Remote-SSH: Connect to Host to bring up a selection list of connections.

スクリーンショット 2020-09-02 16.25.54.png

If you select ʻAdd New SSH Host from here, you will be prompted to enter the shsh command, so enter the same command as for ssh connection from the terminal. When connecting for the first time, I remember that there was only an item of ʻAdd New SSH Host.

スクリーンショット 2020-09-02 16.26.12.png

For key authentication, specify the key with the -i option when you type the ssh command.

These settings are written in ~ / .ssh / config by default. You can also write port forwarding settings, key file settings, etc. directly. (Maybe adding a host)

Connect

Select the remote server from Remote-SSH: Connect to Host on the command palette, just as you did when you added the host. Alternatively, you can access it by selecting the host from "Remote Explorer" in the shortcut menu on the left side of VS Code.

Operate Docker container on remote server

After connecting to the remote server, it's time to connect to the Docker container.

I referred to the following article (I just traced it as it is for reference, so please check the reference source). Operate Docker container on server with SSH from VS Code: https://qiita.com/_kani/items/1ce36e36b0bcda403be2

Container launch

Launch the VS Code terminal and launch the container with docker commands. The RemoteDevelopment Extension also has a function that supports from launching the docker container to connecting, but this seems to be a docker operation on the local machine. So, when setting up a container on a remote server, use the normal docker command (I just couldn't do it?).

Preparing for connection

Port settings

Set up port forwarding. (I don't really understand how this works ...)

Open the ssh config. If you open the command palette and enter ssh config etc., items will appear, so select it.

スクリーンショット 2020-09-02 16.57.32.png

Add the LocalForward setting to the relevant remote host settings.

Host 192.168.*******
  HostName 192.168.*******
  IdentityFile ~/.ssh/id_rsa
  User *****
  LocalForward 23750 /var/run/docker.sock

Parameter settings for docker extensions on remote host

Connect to a remote host from VS Code. It is also necessary to install the Remote Development extension on the remote host.

From Preferrence, set the parameters as follows. Select Remote on the tab and search for Docker.host and it will come up.

tcp://localhost:23750

スクリーンショット 2020-09-02 17.03.22.png

You are now ready.

The setting screen will appear when you enter preferences Open User Setting etc. from the command palette.

Connect

Operate from Remote Explorer in the sidebar. When you select Containers from the pull-down menu at the top, a list of containers will appear, so select the container you want to connect to. Connect to the container with "Attach to Container" (a new window will be launched).

スクリーンショット 2020-09-02 17.14.00.png

Using the debugger

Since VSCod is an integrated development environment, you can debug by setting breakpoints interactively. However, in a specific environment, it has been confirmed that breakpoints cannot be set and steps cannot be executed, and the program runs to the end.

Reference: Problem that does not stop at breakpoint when debugging Python virtual environment with VS Code)

This is a problem, but it seems that the debugger function is enabled by setting parameters in the debugger configuration file'launch.json'.

Add parameters

'launch.json' is displayed on the editor screen by opening the debugger from the side menu of VS Code and pressing the gear mark at the top. Add parameters to this json file.

"stopOnEntry":true

スクリーンショット 2020-09-02 17.26.26.png

Caution

You need to be careful about the order in which you put stopOnEntry. When I put it before ʻargs, I got stuck for a while when I encountered the phenomenon that the ʻargs parameter was not applied and the argument was not passed to the debugger.

Summary

After some trial and error, I have now settled on the above procedure. If you know a better way, please let me know.

I'm particularly worried that I don't really understand how to operate Docker containers.

Recommended Posts

Personal best practices for VS Code-fronted Python development environments
Personal best practices for putting command line launch Python projects in Docker containers
[For organizing] Python development environment
Personal notes for python image processing
Python development environment options for May 2020
Emacs settings for Python development environment
AWS Lambda Development My Best Practices
Python development environment for macOS using venv 2016
Self-style Raspeye Remote Development Settings Best Practices
6 Python libraries for faster development and debugging
Python App Dependent Package Management Best Practices
Personal best practices when fine-tuning with Chainer
Building a Python development environment for AI development
Best practices for Django views.py and urls.py (?)
Best practices for messing with data with pandas
Wagtail is the best CMS for Python! (Perhaps)
[2020 version] Development procedure for personal crawlers and precautions
Set up TinyGo development environment for VS Code
Understand Python for Pepper development. -Introduction to Python Box-