Archive for the 'Python' Category

Python: AttributeError: ‘getopt’ module has no attribute ‘GetoptError’

Wednesday, July 28th, 2010

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.