18

I am new to Python and work with Microsoft Visual Studio

I have to run this (but it says I need more than 1 value):

from sys import argv

script, first, second, third = argv

print "The script is called:", script
print "Your first variable is:", first
print "Your second variable is:", second
print "Your third variable is:", third

I understood that I have to type that (for example) in order to run the code:

python ex13.py first 2nd 3rd

but where do I need to write it?

In the Visual Studio there is only a Start Button for running the script.

5 Answers 5

16

You can use the Python Tools for Visual Studio plugin to configure the python interpreter. Create a new python project and then go to Project Properties | Debug and enter your arguments. You don't need to type python or your script name, only the parameters. Specify the script in General | Startup File. Click Start Debugging to run your script with the parameters specified.

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

2 Comments

is there a way without a project? you can just open .py files in studio and run/debug them - but i found no option to set a command line
Dead link. Is that this extension: github.com/microsoft/PTVS ?
11

You can enter your command line options by doing the following:

  1. In the Solution Explorer, right click on your project and choose Properties

  2. Click on the Debug tab

  3. In Script Arguments, enter your command line options

enter image description here

Now when you run the project it will run with your command line options.

For example, my code has:

opts, args = getopt.getopt(argv,"p:n:",["points=","startNumber="])

In the Script Arguments, I enter -p 100, -n 1

I am using Visual Studio 2017.

3 Comments

Btw, the Debug tab is vertically along the left column of in Property Pages view (not Properties box; from Properties box right click, then choose View Property Pages.)
This is also where one adds command-line arguments to the interpreter too, as well as environment variables.
this setting works per 1 startup file per project. what if i want to run 3 python files with different arguments within the same project?
6

I wrote a example. For every Argument, you test for correct parameter in the for loop. You can put the parameters in the propertys dialog of your project. Under debug, it is the Script Arguments "-i aplha.txt" for example.

import sys
import getopt

def main(argv):
    try:
        opts, args = getopt.getopt(argv,"hi:",["ifile="])
    except getopt.GetoptError:
      print 'test.py -i <inputfile>'
      sys.exit(2)
    for opt, arg in opts:
        if opt in ("-i", "--ifile"):
            inputfile = arg
    print 'Input file is "', inputfile

if __name__ == "__main__":
   main(sys.argv[1:])

Comments

1

In the Visual Studio, you can open a terminal (View -> Terminal). From there you can quickly run it with arguments.

python <your_script.py> <args>

Comments

0

This is for Visual Studio 2022.

In the Solution Explorer, right-click the Python file, then select Add Debug Configuration (or Open Debug and Launch Settings, if that is available).

Solution Explorer screenshot

Now, in the json config file that opens, add your arguments to the appropriate key

{
  "version": "0.2.1",
  "defaults": {},
  "configurations": [
    {
      "type": "python",
      "interpreter": "(default)",
      "interpreterArguments": "",
      "scriptArguments": "myArgument1 myArgument2",
      "env": {},
      "nativeDebug": false,
      "webBrowserUrl": "",
      "project": "setup.py",
      "projectTarget": "",
      "name": "setup.py"
    }
  ]
}

Quick and dirty solution

Add the following to the top of the script (right below any from __future__ import if you have these)

import sys
sys.argv.extend(["myArgument1", "myArgument2"])

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.