28
Jul
2010
Python: AttributeError: ‘getopt’ module has no attribute ‘GetoptError’
By Eric Downing. Filed in Python, Scripting |So I was figuring out how to parse command line arguments with python and using the getopt module with the following code
#!/usr/bin/env python
import sys
import getopt
def main(argv):
infile = "some.xml"
try:
opts,args = getopt.getopt(argv,"hi:d",["help","infile="]
except getopt.GetoptError:
usage()
sys.exit(2)
if __name__ == "__main__":
main(sys.argv[1:])
And I was getting the following error:
AttributeError: ‘getopt’ module has no attribute ‘GetoptError’
So my first problem was that I had named my script getopt.py and after renaming the script, I got the same error. Well you now need to remove the getopt.pyc file that was generated from your earlier running of the poorly named script.
Thursday, July 28th 2011 at 5:27 pm
Thank you so much for this. I did the exact same thing. Renaming the .py file and removing .pyc file worked perfectly.
Tuesday, January 20th 2015 at 6:22 pm
thanks.. i figured out the ‘getopt.py’ renaming part, but the remnant ‘getopt.pyc’ tricked me into 30minutes of debugging puzzelment.. Thank you for the hint.
Thursday, January 29th 2015 at 12:52 am
You are welcome! I don’t remember how much time it took me to figure this out when I started.
Sunday, July 10th 2016 at 3:29 am
Duuuuude, I’m so glad you posted this! Thanks, you saved me some wasted time 😀