Simple Python Syntax Problem

Posted on 2010-08-23
Last Modified: 2012-05-10
Hi the following code gives me the error below.  I have never messed with python so any input would be appreciated.


includes# ./
Traceback (most recent call last):
  File "./", line 45, in <module>
  File "./", line 18, in main
    except getopt.GetoptError:
NameError: global name 'getopt' is not defined


# import MySQL module

import MySQLdb

# import sys module for argument processing etc

import sys

def main(argv):

    fileName = "unknown"

    hfJobId = 444

    message = "hylafax update"

    type = "FAX"

    sendto = 555

    action = "update"


        opts, args = getopt.getopt(argv, "ha:", ["help"])

    except getopt.GetoptError:



    for opt, arg in opts:

       if opt in ("-h", "--help"):



       elif opt == '-a':

         action = arg

    print "action arg is", action

  # connect

    db = MySQLdb.connect(host="", user="hylafax", passwd="pass",


  # create a cursor

    cursor = db.cursor()

  # execute SQL statement

    cursor.execute("""INSERT INTO lablynk_send_log (fileName, hfJobId, message, type, sendto) VALUES ("test",

    "test","test", "FAX", "test")""")

  # get ID of last inserted record

    print "ID of inserted record is ", int(cursor.insert_id())

if __name__ == "__main__":


def usage():

    print "build usage messages"

Open in new window

Question by:sean-keys
1 Comment
LVL 13

Accepted Solution

Superdave earned 500 total points
ID: 33506125
you should put a
import getopt
at the top somewhere.

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now