簡體   English   中英

導入后Python getopt模塊錯誤“NameError:名稱'opts'未定義”

[英]Python getopt module error "NameError: name 'opts' is not defined" after importing

我試圖從控制台接受兩個參數。 以下代碼似乎在我同事的計算機上運行,​​所以我不確定為什么在嘗試在我的計算機上運行它時它會給我一個錯誤。 我在 Mac 上。

import getopt
import sys
    
   
question_id= None
arg_student = None
argv = sys.argv[1:]
print("test")

try:
    opts, args = getopt.getopt(argv, "i:s:", ["question_id=","arg_student="])
except:
    print("Error")

for opt, arg in opts:
    if opt in ['-i', '--question_id']:
        question_id = arg
    elif opt in ['-s', '--arg_student']:
        arg_student = arg

print("Question Number: " + question_id)        
print("Student response: " + arg_student)

這是我得到的錯誤:

   Error
Traceback (most recent call last):
  File "/Users/ailanysmacbook/github/AutomatedEssayGrading/AutomatedEssayGrading/input.py", line 1, in <module>
    import getopt
  File "/Users/ailanysmacbook/github/AutomatedEssayGrading/AutomatedEssayGrading/getopt.py", line 20, in <module>
    for opt, arg in opts:
NameError: name 'opts' is not defined

在我嘗試導入它之后似乎正在發生。 我需要安裝一些東西嗎? 我不確定缺少什么。

這就是我要放入控制台的內容:

python3 input.py -i 0 -s Sample text.

回答:

我在同一個文件夾中有另一個名為 getopt.py 的文件,它覆蓋了默認的 python 模塊。 將其重命名為 input.py 解決了這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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