Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass command line arguments to Python from VS in Debug mode?

I am working with Python Tools for Visual Studio. (Note, not IronPython.)

I need to work with arguments passed to the module from the command line. I see how to start the module in Debug by right-clicking in the code window and selecting "Start with Debugging". But this approach never prompts me for command line arguments, and len(sys.argv) always == 1.

How do I start my module in debug mode and also pass arguments to it so sys.argv has more than 1 member?

like image 671
philologon Avatar asked Feb 09 '16 21:02

philologon


People also ask

How do you pass arguments in Visual Studio debugging?

To set command-line arguments in Visual Studio, right click on the project name, then go to Properties. In the Properties Pane, go to "Debugging", and in this pane is a line for "Command-line arguments." Add the values you would like to use on this line. They will be passed to the program via the argv array.

How do you debug Python command line arguments?

Go to your project properties, either by right-clicking on the project and picking "Properties" or by picking Properties from the Project menu. Click on Debug, then enter your arguments into the "Script Arguments" field. Save.

How do I pass a command line argument to a Python script?

In Python, arguments are passed to a script from the command line using the sys package. The argv member of sys ( sys. argv ) will store all the information in the command line entry and can be accessed inside the Python script. Python's getopt module can also be used to parse named arguments.


2 Answers

The steps are shown in the image linked here:

image

  1. Go to debug mode in VS Code
  2. Click on the settings icon (gear icon). If it does not exist this will create a launch.json
  3. In the json, in any of the configuration, add the args json parameter:
{
    "name": "Python: Terminal (integrated)",
    "type": "python",
    "request": "launch",
    "stopOnEntry": true,
    "pythonPath": "${config:python.pythonPath}",
    "program": "${file}",
    "cwd": "",
    "console": "integratedTerminal",
    "env": {},
    "args": [
        "input2.csv",
        "output2.csv"
    ],
    "envFile": "${workspaceFolder}/.env",
    "debugOptions": [],
    "internalConsoleOptions": "neverOpen"
}

Make sure you choose that environment while debugging

like image 127
NN2 Avatar answered Oct 22 '22 18:10

NN2


  1. Go to your project properties, either by right-clicking on the project and picking "Properties" or by picking Properties from the Project menu.

  2. Click on Debug, then enter your arguments into the "Script Arguments" field.

  3. Save.

Screenshot of Visual Studio dialog

like image 20
jgfооt Avatar answered Oct 22 '22 18:10

jgfооt