Friday, April 6, 2012

Install MySQL for Python (MySQLdb) on Windows 7

I loved it how people praise python as being so simple, but lack to explain the fact that they either download pre-compiled packages and libraries, and/or use linux. Lackluster is what comes to mind when I refuse to download this (http://code.google.com/p/soemin/downloads/detail?name=MySQL-python-1.2.3.win32-py2.7.exe) and rather try to compile from scratch on windows 7 (using python 2.7, mysql community 5.5).

Given that I've put in more than 15 minutes to get this rolling, I thought I would blog about it. There are other options online, but nothing better than documenting your failures, right? :)

1) First step is installing mysql *with* the development tools. Also the Mysql Connector C (http://dev.mysql.com/downloads/connector/c/).
2) Next up is installation of the setuptools (http://pypi.python.org/pypi/setuptools). And while there is a windows executable, that didn't work for me. So download the ez_setup.py and it should download/update your lib.
3) Check if your site.cfg file has the correct registry key for your version of mysql server (registry_key = SOFTWARE\MySQL AB\MySQL Server 5.5).
4) Also double-check if your mingw path is updated in your environment variables (computer >> properties >> advanced system settings >> environment variables >> ADD to your path "C:\MinGW\bin")
5) You need to comment out the lines regarding to the static build in setup_windows.py, as well as instructing the compiler to use the dynamic lib version (add 'client = "libmysql"').

    # XXX static doesn't actually do anything on Windows
    #if enabled(options, 'embedded'): B
    #    client = "mysqld"
    #else:
    #    client = "mysqlclient"
    client = "libmysql"

6) If you are lucky, running python setup.py build –compile=mingw32 will work. I wasn't as lucky since I got a missing 'config-win.h'. (Check this out: http://stackoverflow.com/questions/1972259/mysql-python-install-problem-using-virtualenv-windows-pip).

3 comments:

  1. Hello,I try to develop an application in python in wich a client TCP send data to server and server make(enregister) this data in a mysql database.
    I like that server and client be distant.
    but my application is true only for local internet.
    Can you help me please.

    ReplyDelete
  2. # serveur attend la connexion d'un client
    import socket, sys
    import MySQLdb
    HOST = '192.168.1.8'
    PORT = 50000
    # 1) création du socket :
    mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # 2) liaison du socket à une adresse précise :
    try:
    mySocket.bind((HOST, PORT))
    except socket.error:
    print ("La liaison du socket à l'adresse choisie a échoué.")
    sys.exit()

    while 1:
    # 3) Attente de la requête de connexion d'un client :
    print ("Serveur prêt, en attente de requêtes ...")
    mySocket.listen(5)
    # 4) Etablissement de la connexion :
    connexion, adresse = mySocket.accept()
    print ("Client connecté, adresse IP %s, port %s" % (adresse[0], adresse[1]))
    # 5) Dialogue avec le client :
    connexion.send("Vous êtes connecté au serveur Marcel. Envoyez vos messages.")
    msgClient = connexion.recv(1024)
    while 1:
    print ("C>", msgClient)
    if msgClient.upper() == "FIN" or msgClient =="":
    break
    msgServeur = raw_input("S> ")
    connexion.send(msgServeur.decode())
    msgClient = connexion.recv(1024)
    # 6) Fermeture de la connexion :
    connexion.send("Au revoir !")
    print ("Connexion interrompue.")
    connexion.close()
    ch = raw_input("ecommencer erminer ? ")
    if ch.upper() =='T':
    break
    ******this is for server the second is for client****

    # Ce client dialogue avec un serveur ad hoc
    # -*- coding: cp1252 -*-
    import socket, sys
    HOST = '192.168.1.8'
    PORT = 50000
    # 1) création du socket :
    mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    #2) envoi d'une requête de connexion au serveur :
    try:
    mySocket.connect((HOST, PORT))
    except socket.error:
    print ("La connexion a échoué.")
    sys.exit()
    print ("Connexion établie avec le serveur.")
    # 3) Dialogue avec le serveur :
    msgServeur = mySocket.recv(1024).decode()
    while 1:
    if msgServeur.upper() == "FIN" or msgServeur =="":
    break
    print ("S>", msgServeur)
    msgClient = input("C> ")
    mySocket.send(msgClient.encode())
    msgServeur = mySocket.recv(1024).decode()
    # 4) Fermeture de la connexion :
    print ("Connexion interrompue.")
    mySocket.close()

    ReplyDelete
  3. Thanks for giving a link to the MySQLdb binary. That's just what I needed.

    ReplyDelete