Reputation: 1527
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
Reputation: 111
You can enter your command line options by doing the following:
In the Solution Explorer, right click on your project and choose Properties
Click on the Debug tab
In Script Arguments, enter your command line options
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
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).
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"
}
]
}
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
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
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
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