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.

4 comments to “Python: AttributeError: ‘getopt’ module has no attribute ‘GetoptError’”

  1. Comment by newbie:

    Thank you so much for this. I did the exact same thing. Renaming the .py file and removing .pyc file worked perfectly.

  2. Comment by daixtr:

    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.

  3. Comment by ecdown:

    You are welcome! I don’t remember how much time it took me to figure this out when I started.

  4. Comment by mykill:

    Duuuuude, I’m so glad you posted this! Thanks, you saved me some wasted time 😀

Leave a Reply