1

suppose I have the following script:

import argparse

parser = argparse.ArgumentParser()

parser.add_argument('-t','--text',
                help="Input a text")
args = parser.parse_args()

def test_function(x):
    y = x
    print(y)

if __name__ == '__main__':
    test_function(args.text)

which I call from the console with

python newtest.py -t hello

Question: In Visual Code, is there a way that I can execute the code from the command line (like shown above), but simultaneously also put a breakpoint at e.g. at the y=x line in the test_function, so that I can debug the script that I have called from the command line?

Right now it is just executed and the breakpoint is ignored, basically it does not stop here: enter image description here

Jean_N
  • 489
  • 1
  • 4
  • 19
  • use `debugpy` module to start script in debugger and then attach to this debugger, how to use `debugpy` see what ms-python types to the command line to debug a py script, you can let debugpy wait till the debugclient attaches – rioV8 Jan 16 '22 at 20:24

1 Answers1

3

Not quite the answer to your question, but my web searching brought me here before I found what I wanted, which was to call the script with arguments and use the vscode debugger. This doesn't debug what you call in the terminal, but instead sets what is called when you run a debug. If you've got a lot of different things in your folder it may be a hassle to try to maintain them all or anything like that, but...

If you go to your launch.json file in the .vscode directory, you'll get your debug configs. You can add a list item for args in there. So, mine looks like this, which then calls whatever python file I'm debugging plus -r asdf

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "args": [
                "-r asdf"
            ],
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true
        }
    ]
}

If you have multiple arguments, you can't put them in the same string, they should be individual, separated elements. Didn't realize this at first, but it is a list after all. So having multiple would be like this:

...
    "args": [
        "-r asdf",
        "-m fdsa"
    ]
...
scotscotmcc
  • 2,719
  • 1
  • 6
  • 29
  • Is there a way to run the debugger for a script with arguments that don't require a letter? (e.g. `python3 myscript.py some arguments`, with a dynamic number of arguments?) – Sam Jan 16 '23 at 20:09
  • you can put whatever you want in the args, so you could have `... "args":["some", "arguments"]`. When you say dynamic, though, when do you want the args to be defined? I have no idea if there is something where you would hit 'run and debug' in vscode and be prompted for your args at that point – scotscotmcc Jan 17 '23 at 00:56