20

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?

3
  • press super (the symbol with the small window); enter cmd; open cmd.exe; go to your project,... :-P Commented Feb 9, 2016 at 21:33
  • At your suggestion I just tried that. It runs the python module, but not in Visual Studio. Any other suggestions? Commented Feb 9, 2016 at 21:38
  • So can anyone think of a workaround to D. Alveno's statement, "It is either one or the other." ? Can I write another .py file that invokes the windows command line to call my real .py file but runs it in debug? Perhaps I can get len(sys.argv) and if it == 1 then try to open a sysargv.txt file? Commented Feb 10, 2016 at 0:39

6 Answers 6

18

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

Sign up to request clarification or add additional context in comments.

2 Comments

I am the debug mode and adding the "args" arguments did not change the arguments passed to the script.
He's not using VS Code, he's using VS.
13
  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

3 Comments

I have been working without a Project -- just the .py file opened in the editor. Is there a way to do it without creating a Project, or is that a required step?
Have you tried passing args directly to the script form the command line? For testing purposed you might want to iterate over sys.argv and print out what it contains.
@jgfооt, thanks so much! This was exactly what I was looking for!
4

I have solved this by putting a breakpoint on the first line of my script and opening the Immediate window in VS (where you can execute commands in the context of your script). Then I run

import sys
sys.argv += 'arg1 arg2 --arg3'.split()

Comments

1

In Visual Studio 2022, under the Debug menu, choose your project's Debug Properties. There you can add command line arguments.

2 Comments

It looks really similar to jgfoot's answer.
For me, the command line arguments are present in both "start with debugging" and "start without debugging", defeating the purpose.
0

You want to select "Execute Project with Python Interactive" from the debug dropdown menu. The keyboard shortcut for this is Shift+Alt+F5. When you do that, you will have a window open at the bottom of the screen called Python Interactive and you will see your printed statements and any prompts for inputs from your program.

This does not allow you to also enter debug mode though. It is either one or the other.

5 Comments

Thank you. I have adjusted the title of this question to reflect my interest in working with command line arguments in debug mode. Sorry I did not make that clearer before. It was my oversight.
In that case, it sounds like VS is running your python file using pythonw.exe instead of python.exe. When you start the program using debugging, in the "Process" (top left of VS), does it say pythonw.exe? If it does, that makes it so a terminal window does not come up while running the program. If you want to pass inputs into your program, you will need to run it using python.exe.
In the console window that comes up it says python.exe as the window title.
I accepted this answer for its last sentence, which could be restated as "you can't pass command line arguments and also run the code in debug mode."
You can use debug mode by following @jgfoot answer
0

1.if you are using anaconda then launch vscode from anaconda navigator.

2.Then open your python file from your folder.

3.Then go to debug option and click on add configuration,then launch.json file will open, in that file there is "console": "integratedTerminal" line, comment this line and add "console":"none"and add "args":["your command line arguments"] and save it.

4.Debug or run your python file. See below link.How to add command line arguments in vscode? https://code.visualstudio.com/docs/python/debugging

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.