Menu

AttributeError: 'module' object has no attrib

Help
Jim
2003-11-02
2012-09-19
  • Jim

    Jim - 2003-11-02

    Hello,

    I am new to Python and new to MySQL. I am trying to run an example I found on the Internet and am getting an error. I have MySQL 4.1 installed, it is running correctly as a service on WinXP Pro (I can run admin commands on a command line), my databases and tables are set up correctly. I have Python 2.3 installed and MySQLdb .92 installed. Here is the example I am trying to run:

    #!/usr/bin/python

    # import MySQL module
    import MySQLdb

    # connect
    db = MySQLdb.connect(host="localhost", user="root", db="church")

    # create a cursor
    cursor = db.cursor()

    # execute SQL statement
    cursor.execute("SELECT * FROM ministers")

    # get the resultset as a tuple
    result = cursor.fetchall()

    # iterate through resultset
    for record in result:
        print record[0] , "-->", record[1]

    and here is my output error:

    Traceback (most recent call last):
      File "D:\ActiveState\Komodo-2.5\callkomodo\kdb.py", line 433, in _do_start
        self.kdb.run(code_ob, locals, locals)
      File "D:\ActiveState\Python22\lib\bdb.py", line 349, in run
        exec cmd in globals, locals
      File "D:\ActiveState\Komodo-2.5\python lessons\MySQLdb.py", line 4, in ?
        import MySQLdb
      File "D:\ActiveState\Komodo-2.5\python lessons\MySQLdb.py", line 7, in ?
        db = MySQLdb.connect(host="localhost", user="root", db="church")
    AttributeError: 'module' object has no attribute 'connect'

    Any help would be greatly appreciated. I don't understand why I get this error. I have the default root account with no password.

    Thanks, Jim

     
    • Greg Fortune

      Greg Fortune - 2003-11-03

      Hmmm, that's a little strange looking.  The mysql module should be installed into site-packages for the python installation, but the last two lines of the traceback look really odd.

      In MySQLdb.py there is an import of MySQLdb.  Did you create a script called MySQLdb and try to use the real MySQLdb module from inside your script?  If so, try changing its name to something like db.py so there aren't any namespace/import problems.

      Note that your user account/password stuff have nothing to do with the error.  The error is an attribute error which means something isn't defined that should be.  Get that MySQLdb script renamed to something else and your problems might all go away :)

       
    • Jim

      Jim - 2003-11-03

      Thanks Greg,

      That was it ... I changed the name of my py script to db.py and it worked. I never thought of a namespace conflict.

      Jim

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.