Remote Debugging with VS Code

Introduction

In this tutorial, we will show how to set up remote debugging with VS Code on the Neuro Platform using the Neu.ro project template.

Remote debugging relies on a running SSH server in a job's 22 port. We ensure it for you if you use our base image (ghcr.io/neuro-inc/base).

Initializing a new project

Make sure you have Neu.ro CLI and cookiecutter installed:

$ pip install pipx
$ pipx install neuro-all cookiecutter
$ neuro login

Then, initialize an empty project:

$ cookiecutter gh:neuro-inc/cookiecutter-neuro-project --checkout release

The project initialization command asks several questions about your project:

project_name [Name of the project]: Neuro VSCode
project_dir [neuro-vscode]:
project_id [neuro-vscode]:
code_directory [modules]: 
preserve Neuro Flow template hints [yes]:

Configuring the project

Add debugpy to your project's requirements.txt file (located in the project's root folder):

neuro-flow

flake8
mypy
isort
black
debugpy

Add the following lines to the beginning of your code file. In this example, it's modules/train.py.

import debugpy
debugpy.listen(("0.0.0.0", 5678))
debugpy.wait_for_client()

Next, configure the project's environment on the Neu.ro Platform:

$ neuro-flow build myimage

When the image is built, you can upload your code from a local file to the platform storage:

$ neuro-flow upload code

By default, this will upload everything from your project's modules folder to the storage:<your_project_id>/modules storage folder. To configure the source and the target for this command, go to your project's .neuro/live.yml file and find the code section under volumes:

volumes:
  data:
    remote: storage:$[[ flow.project_id ]]/data
    mount: /project/data
    local: data
  code:
    remote: storage:$[[ flow.project_id ]]/modules
    mount: /project/modules
    local: modules
  config:
    remote: storage:$[[ flow.project_id ]]/config
    mount: /project/config
    local: config
    read_only: True

Here, you can specify your local code folder and the storage folder you want to upload this code to.

Running your code

In this example, we will be running a training job based on the code contained in the train.py file we just uploaded to the platform storage. To do this, run:

$ neuro-flow run train

Once the job is running, detach from it by pressing Ctrl+P, Ctrl+Q and run the following command:

$ neuro job port-forward <job-id> 5678:5678

This will allow you to access the job by the 5678 port.

Debugging

Open your code file in VS Code and navigate to Run > Start Debugging or press F5:

Select Remote Attach:

Enter localhost as the host name and the job's port number (in this case, it's 5687):

When this is done, you can set the breakpoint and start debugging.

Last updated