0

I want to debug a Python module which I have to execute like this:

python -m ModuleA.ModuleB <someargs>

How can I configure the -m switch on VS Ccode's launch.json? The args section is for the script being executed, not for the python binary itself.

1 Answer 1

2

The Python launch configuration accepts a "module" instead of a "program". It is used in debugging specific Python applications such as Flask, Django, and Scrapy. Here is the sample for a Flask app which seems similar to what you want:

{
    "name": "Python: Flask",
    "type": "python",
    "request": "launch",
    "module": "flask",            # <---------------------
    "env": {
        "FLASK_APP": "app.py"
    },
    "args": [
        "run",
        "--no-debugger",
        "--no-reload"
    ],
    ...
}

As you can see, this configuration specifies "env": {"FLASK_APP": "app.py"} and "args": ["run", "--no-debugger","--no-reload"]. The "module": "flask" property is used instead of program.

So, given this sample workspace with folder Q containing ModuleA.ModuleB:

$ tree .
.
├── Q
│   └── ModuleA
│       ├── __init__.py
│       └── ModuleB.py
└── .vscode
    └── launch.json

where ModuleB is:

import sys

if __name__ == "__main__":
    print(*sys.argv)

You can specify this launch.json configuration as:

{
    "name": "run-specific-module",
    "type": "python",
    "request": "launch",
    "console": "integratedTerminal",
    "cwd": "${workspaceFolder}/Q",
    "module": "ModuleA.ModuleB",
    "args": [
        "123",
        "abc",
    ]
},

Running that gives this output in the terminal:

$  cd /path/to/Q ; env /path/to/python /path/to/.vscode/extensions/ms-python.python-2020.6.91350/pythonFiles/lib/python/debugpy/launcher 37343 -- -m ModuleA.ModuleB 123 abc 
('/path/to/Q/ModuleA/ModuleB.py', '123', 'abc')

which should be the same as:

$ python -m ModuleA.ModuleB 123 abc
('/path/to/Q/ModuleA/ModuleB.py', '123', 'abc')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.