简体   繁体   English

在 Microsoft Visual Studio 中使用 arguments 运行 python 脚本

[英]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: 您可以通过执行以下操作输入命令行选项:

  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 运行该项目

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.

相关问题 在 Visual Studio 本地网站上运行 Python 脚本 - Running Python script on Visual Studio local website 使用 Visual Studio Code 中的参数运行 Python 程序 - Running a Python program with arguments from within the Visual Studio Code Visual Studio Code:如何使用参数调试 Python 脚本 - Visual Studio Code: How debug Python script with arguments Visual Studio Code 终端在 Powershell 中继续运行 Python 脚本 - Visual Studio Code Terminal keeps running Python script in Powershell 在 Visual Studio Code 中运行 python 脚本; 如何让 `input ()` 工作? - Running python script in Visual Studio Code; how to get `input ()` to work? Python计划脚本在IDLE中运行,但不在Visual Studio Code中运行 - Python schedule script running in IDLE but not in Visual Studio Code 将参数传递给正在运行的 python 脚本 - Passing arguments to a running python script 在Microsoft Visual Studio上运行Python将.xlsx文件编辑为CSV,Openpyxl是否可以? - Running Python on Microsoft Visual Studio to edit .xlsx files into CSV, is it possible with Openpyxl? python 项目未在 Visual Studio 中运行 - python project is not running in visual studio Python应用程序未在Visual Studio中运行 - Python Application not running in visual studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM