Running Python script with arguments in Microsoft Visual Studio
J

5

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.

Jauregui answered 7/8, 2014 at 9:48 Comment(0)
D
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:])
Demobilize answered 7/8, 2014 at 13:8 Comment(0)
I
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.

Ilanailangilang answered 7/8, 2014 at 10:56 Comment(2)
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 lineAbdomen
Dead link. Is that this extension: github.com/microsoft/PTVS ?Butta
G
10

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.

Gilboa answered 3/2, 2018 at 1:19 Comment(3)
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.)Ruckman
This is also where one adds command-line arguments to the interpreter too, as well as environment variables.Ruckman
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?Opalopalesce
D
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:])
Demobilize answered 7/8, 2014 at 13:8 Comment(0)
P
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>
Prorogue answered 30/3, 2022 at 13:8 Comment(0)
E
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"])
Electrotherapeutics answered 1/5, 2023 at 17:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.