在 Microsoft Visual Studio 中运行带参数的 python 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25179304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:49:54  来源:igfitidea点击:

Running python script with arguments in microsoft visual studio

pythonvisual-studio

提问by axcelenator

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

谢谢

采纳答案by Thor

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.

我写了一个例子。对于每个参数,您在 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:])

回答by smn

You can use the Python Tools for Visual Studioplugin 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 pythonor your script name, only the parameters. Specify the script in General | Startup File. Click Start Debugging to run your script with the parameters specified.

您可以使用Python Tools for Visual Studio插件来配置 Python 解释器。创建一个新的 python 项目,然后转到 Project Properties | 调试并输入您的参数。您不需要键入python或脚本名称,只需输入参数。在 General | 中指定脚本 启动文件。单击开始调试以使用指定的参数运行脚本。

回答by KRock

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

  1. 在解决方案资源管理器中右键单击您的项目并选择属性。

  2. 单击调试选项卡

  3. 在脚本参数中输入您的命令行选项

  4. 运行项目

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。