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:
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"
]
...