bones.felipe
bones.felipe

Reputation: 596

How to specify a module (-m) when using the Python debugger?

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.

Upvotes: 0

Views: 130

Answers (1)

Gino Mempin
Gino Mempin

Reputation: 29608

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')

Upvotes: 2

Related Questions