[英]Running python script with arguments in microsoft visual studio
我是 python 的新手,使用 Microsoft Visual Studio
我必须运行这个(但它说需要超过 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
我知道我必须输入(例如)才能运行代码:
python ex13.py first 2nd 3rd
但是我需要在哪里写呢?
在 Visual Studio 中只有用于运行脚本的开始按钮
谢谢
您可以使用Python Tools for Visual Studio插件来配置python解释器。 创建一个新的python项目,然后转到Project Properties | 调试并输入您的参数。 您不需要键入python
或脚本名称,只需键入参数。 在General |中指定脚本 启动文件。 单击“开始调试”以使用指定的参数运行脚本。
我写了一个例子。 对于每个Argument,您在for循环中测试正确的参数。 您可以将参数放在项目的“属性”对话框中。 在调试下,例如,脚本参数“-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:])
您可以通过执行以下操作输入命令行选项:
右键单击解决方案资源管理器中的项目,然后选择“属性”。
单击“调试”选项卡
在脚本参数中输入命令行选项
运行该项目
例如我的代码有:
opts, args = getopt.getopt(argv,"p:n:",["points=","startNumber="])
在脚本参数中我输入-p 100, -n 1
我正在使用Visual Studio 2017。
在 Visual Studio 中,您可以打开一个终端(查看 -> 终端)。 从那里您可以使用 arguments 快速运行它。
python <your_script.py> <args>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.