簡體   English   中英

在 Microsoft Visual Studio 中使用 arguments 運行 python 腳本

[英]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:])

您可以通過執行以下操作輸入命令行選項:

  1. 右鍵單擊解決方案資源管理器中的項目,然后選擇“屬性”。

  2. 單擊“調試”選項卡

  3. 在腳本參數中輸入命令行選項

  4. 運行該項目

例如我的代碼有:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM