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

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 解决了这个问题。