axcelenator
axcelenator

Reputation: 1527

Running Python script with arguments in Microsoft Visual Studio

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.

Upvotes: 18

Views: 42654

Answers (5)

KRock
KRock

Reputation: 111

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.

Upvotes: 11

user7610
user7610

Reputation: 28879

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"])

Upvotes: 0

crazzy
crazzy

Reputation: 169

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>

Upvotes: 1

Thor
Thor

Reputation: 158

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:])

Upvotes: 6

smn
smn

Reputation: 558

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.

Upvotes: 16

Related Questions