> For the complete documentation index, see [llms.txt](https://docs.apolo.us/index/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apolo.us/index/apolo-console/apps/pre-installed/jobs/remote-debugging-with-vs-code.md).

# Remote Debugging with VS Code

## Introduction

In this tutorial, we will show how to set up remote debugging with VS Code on the platform using the flow template.

{% hint style="warning" %}
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`).
{% endhint %}

## Initializing a new project

Make sure you have CLI and [**cookiecutter**](https://github.com/cookiecutter/cookiecutter) installed, refer to [https://github.com/neuro-inc/apolo-documentation/blob/master/apolo-console/first-steps/getting-started.md](https://github.com/neuro-inc/apolo-documentation/blob/master/apolo-console/first-steps/getting-started.md "mention") for instructions.

Then, initialize an empty flow:

```bash
$ 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]: Apolo VSCode
project_dir [apolo-vscode]:
project_id [apolo-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):

```bash
apolo-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`.

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

Next, configure the project's environment on the platform:

```bash
$ apolo-flow build myimage
```

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

```
$ apolo-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:

```
$ apolo-flow run train
```

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

```
$ apolo 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**:

![](https://3952707095-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUD8kiAsnN8MKP7nzsJRQ%2Fuploads%2Fgit-blob-b9eeab30be693bd12041599cb70952491c122b77%2Fimage%20\(89\)%20\(1\).png?alt=media)

Select **Remote Attach**:

![](https://3952707095-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUD8kiAsnN8MKP7nzsJRQ%2Fuploads%2Fgit-blob-1b0d8fe97477c9e12173c8e4f066107ddd3badb2%2Fimage%20\(88\).png?alt=media)

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

![](https://3952707095-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUD8kiAsnN8MKP7nzsJRQ%2Fuploads%2Fgit-blob-6d37de3ac52c8b011562d1a70ac324cc740e1a35%2Fimage%20\(87\)%20\(1\).png?alt=media)

![](https://3952707095-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUD8kiAsnN8MKP7nzsJRQ%2Fuploads%2Fgit-blob-cc02fcfd2c69daaff42ba2b9c82e1e006365e1b8%2Fimage%20\(91\).png?alt=media)

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