简体   繁体   中英

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

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.

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

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM