Search code examples
pythonvisual-studioargumentscommand-line-argumentsvisual-studio-debugging

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.


Solution

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