[英]Running python script with arguments in microsoft visual studio
I am new to python and work with Microsoft Visual Studio我是 python 的新手,使用 Microsoft Visual Studio
I have to run this(but it says need more than 1 value):我必须运行这个(但它说需要超过 1 个值):
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在 Visual Studio 中只有用于运行脚本的开始按钮
Thanks谢谢
You can use the Python Tools for Visual Studio plugin to configure the python interpreter. 您可以使用Python Tools for Visual Studio插件来配置python解释器。 Create a new python project and then go to Project Properties | 创建一个新的python项目,然后转到Project Properties | Debug and enter your arguments. 调试并输入您的参数。 You don't need to type python
or your script name, only the parameters. 您不需要键入python
或脚本名称,只需键入参数。 Specify the script in General | 在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. 对于每个Argument,您在for循环中测试正确的参数。 You can put the parameters in the propertys dialog of your project. 您可以将参数放在项目的“属性”对话框中。 Under debug, it is the Script Arguments "-i aplha.txt" for example. 在调试下,例如,脚本参数“-i aplha.txt”。
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: 您可以通过执行以下操作输入命令行选项:
Right click on your project in the solution explorer and choose Properties. 右键单击解决方案资源管理器中的项目,然后选择“属性”。
Click on the Debug Tab 单击“调试”选项卡
In script arguments enter your command line options 在脚本参数中输入命令行选项
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
在脚本参数中我输入-p 100, -n 1
I am using Visual Studio 2017. 我正在使用Visual Studio 2017。
In the Visual Studio, you can open a terminal (View -> Terminal).在 Visual Studio 中,您可以打开一个终端(查看 -> 终端)。 From there you can quickly run it with arguments.从那里您可以使用 arguments 快速运行它。
python <your_script.py> <args>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.