Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make VScode launch.json for a Python module

I'm researching self-supervised machine learning code.

And I have wanted to debug the code with python debugger not pdb.set_trace(). This is python command for ubuntu terminal.

python -m torch.distributed.launch --nproc_per_node=1 main_swav.py \
--data_path /dataset/imagenet/train \
--epochs 400 \
--base_lr 0.6 \
--final_lr 0.0006 \
--warmup_epochs 0 \
--batch_size 8 \
--size_crops 224 96 \
--nmb_crops 2 6 \
--min_scale_crops 0.14 0.05 \
--max_scale_crops 1. 0.14 \
--use_fp16 true \
--freeze_prototypes_niters 5005 \
--queue_length 380 \
--epoch_queue_starts 15\
--workers 10

In order to debug the code with VScode, I tried to revise launch.json like below as referring stackoverflow question

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "module": "torch.distributed.launch --nproc_per_node=1 main_swav.py",
            "request": "launch",
            "console": "integratedTerminal",
            "args": ["--data_path", "/dataset/imagenet/train"]
        }
    ]
}

I knew this would not work...

Could you give me some advice?

like image 769
JH S Avatar asked Nov 17 '25 03:11

JH S


2 Answers

Specify the module you want to run with "module": "torch.distributed.launch"

You can ignore the -m flag. Put everything else under the args key.

Note: Make sure to include --nproc_per_node and the name of file (main_swav.py) in the list of arguments

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "debugpy",
            "module": "torch.distributed.launch",
            "request": "launch",
            "console": "integratedTerminal",
            "args": [
                "--nproc_per_node", "1", 
                "main_swav.py",
                "--data_path", "/dataset/imagenet/train",
            ]
        }
    ]
}

Read more here: https://code.visualstudio.com/docs/python/debugging#_module

like image 87
Matt Spataro Avatar answered Nov 18 '25 18:11

Matt Spataro


This is an example of my launch.json that I use to debug Python modules.

It has an additional configuration to debug "current file" (not as module) which is useful to keep.

{
linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python: Module",
      "type": "python",
      "request": "launch",
      "module": "path.to.module",
      "args": ["run_example --arg <arg>"],
      "justMyCode": true
    },
    {
      "name": "Python: Current File",
      "type": "python",
      "request": "launch",
      "program": "${file}",
      "console": "integratedTerminal",
      "justMyCode": true
    }
  ]
}

This would replicate a terminal command to run a Python module like so:

python -m path.to.module run_example --arg <arg>
like image 39
azizbro Avatar answered Nov 18 '25 16:11

azizbro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!