python getopt模块错误&quot'nameError:name' opts'不是定义的导入后
我试图从控制台中汲取两个参数。 以下代码似乎对我的同事的计算机有效,所以我不确定为什么在试图在我的矿山上运行它时会给我一个错误。我在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.
I'm trying to take in two arguments from the console. The following code seems to have worked on my colleague's computer, so I'm not sure why it is giving me an error when trying to run it on mine. I am on a 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)
This is the error I am getting:
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
It seems to be happening right after I try importing it. Do I need to install something? I'm not sure what's missing.
This is what I'm putting into the console:
python3 input.py -i 0 -s Sample text.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答:
我在同一文件夹中有另一个名为getopt.py的文件,该文件覆盖了默认的Python模块。将其重命名为Input.py修复了此问题。
Answer:
I had another file called getopt.py in the same folder which was overriding the default python module. Renaming it to input.py fixed this issue.