add tag
Pax
I have a CLI app and I want to debug it using VS Code (Code).

I know there's a "Debug current file" option in Code but since I have to pass the actual CLI command, just doing "Debug current file" (in this case the entry point of my CLI app", it won't work.


The entry-point of my CLI app:

```
# run.py

import load_env  # noqa
from cli.main import app

if __name__ == "__main__":
    app()
```

I want to debug what happens when I run the CLI command, e.g

`python run.py appeal --start-month 1`

...where `appeal --start-month 1` is the CLI command + command options.
Top Answer
Pax
**TL;DR** `python -m debugpy --listen 5678 --wait-for-client run.py appeal --start-month 1`

---
  
As found in [VS Code docs on CLI debugging](https://code.visualstudio.com/docs/python/debugging#_command-line-debugging)

> `--listen or --connect [<host>:]<port>` --- **Required.** Specifies the host address and port for the debug adapter server to wait for incoming connections (--listen) or to connect with a client that is waiting for an incoming connection (--connect). This is the same address that is used in the VS Code debug configuration. By default the host address is localhost (127.0.0.1).
>
> `--wait-for-client	none`	--- **Optional.** Specifies that the code should not run until there's a connection from the debug server. This setting allows you to debug from the first line of your code.
  

I had to `pip install debugpy` to be able to run the command so I can debug it.

Also, IIRC, I had to use the `--wait-for-client` option otherwise my script simply executes without stopping at the breakpoints. This might just be because of a specific detail in my setup.

This room is for discussion about this question.

Once logged in you can direct comments to any contributor here.

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.