python getopt模块错误&quot'nameError:name' opts'不是定义的导入后

发布于 2025-01-21 17:21:46 字数 1117 浏览 2 评论 0原文

我试图从控制台中汲取两个参数。 以下代码似乎对我的同事的计算机有效,所以我不确定为什么在试图在我的矿山上运行它时会给我一个错误。我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恋竹姑娘 2025-01-28 17:21:47

答:

我在同一文件夹中有另一个名为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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文