'Python getopt module error "NameError: name 'opts' is not defined" after importing
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.
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ailany |