axc*_*tor 11 python 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
Run Code Online (Sandbox Code Playgroud)
我知道我必须键入(例如)才能运行代码:
python ex13.py first 2nd 3rd
Run Code Online (Sandbox Code Playgroud)
但我需要在哪里写呢?
在Visual Studio中,只有用于运行脚本的"开始"按钮
谢谢
小智 14
您可以使用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:])
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以通过执行以下操作来输入命令行选项:
在解决方案资源管理器中右键单击您的项目并选择属性。
单击调试选项卡
在脚本参数中输入您的命令行选项
运行项目
例如我的代码有:
opts, args = getopt.getopt(argv,"p:n:",["points=","startNumber="])
Run Code Online (Sandbox Code Playgroud)
在我输入的脚本参数中 -p 100, -n 1
我正在使用 Visual Studio 2017。