Debugging Python programs that require CLI arguments in VSCode

Debugging Python programs that require CLI arguments in VSCode

Command Palette (CTRL+SHIFT+P): Debug: Add Configuration

Command Palette Debug Config.png

Then, select “Python Debugger”

Command Palette Python Debugger.png

Then, select “Python File with Arguments”

Command Palette Python File with Arguments.png

VSCode then creates the .vscode directory in your project, if it doesn’t exist already, and adds launch.json as follows:

{
    // 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": [
        {
            "name": "Python Debugger: Current File with Arguments",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "args": "${command:pickArgs}"
        }
    ]
}

If you don’t change anything, when you debug a file VSCode will prompt you for arguments to pass the file every time you run the debugger.

VSCode Prompt for Arguments.png

Can also click, bottom right, “Add Configuration” which will stub out adding more configs in the JSON so that you can tailor it to your specific application. For example:

{
    // 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": [
        {
            "name": "Blue Horseshoe Debugger",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "args": ["run", 
            "--players", "5", 
            "--duration", "2", 
            "--round-end", "fixed", 
            "--config", "strategy_config.yaml", 
            "--simulation", "true", 
            "--strategies", "conservative,conservative,conservative,momentum,arbitrage"]
        },
        {
            "name": "Python Debugger: Current File with Arguments",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "args": "${command:pickArgs}"
        }
    ]
}

links