Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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 Start Button for running the script

Thanks

like image 305
axcelenator Avatar asked Aug 07 '14 09:08

axcelenator


People also ask

How do I run a Python script with an argument in Visual Studio code?

indicate your specific arguments. After all, you can execute your created task with Ctrl + Shift + B and choose the new "Run Python with argument" task.

Can I run a Python script in Visual Studio?

Visual Studio provides open-source support for the Python language through the Python Development and Data Science workloads (Visual Studio 2017 and later) and the free Python Tools for Visual Studio extension (Visual Studio 2015 and earlier). Visual Studio doesn't support Python on Mac now.


3 Answers

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.

like image 80
smn Avatar answered Oct 03 '22 04:10

smn


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:])
like image 24
Thor Avatar answered Oct 03 '22 06:10

Thor


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

  1. Right click on your project in the solution explorer and choose Properties.

  2. Click on the Debug Tab

  3. In script arguments enter your command line options

  4. Run the project

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.

like image 25
KRock Avatar answered Oct 03 '22 06:10

KRock