Friday, January 13, 2012

Python-MySQL-Windows

One of the best parts of programming is you are always learning something new. I happened to build a simple statistics application with MySQL as the database. For practice , I decided to revert to Python since doing lot of coding in PHP and Perl. I was lazy enough to decide to add MySQL connectivity to Python on my Windows 7 (32-bit) box and found that I had a range of issues.

To cut the long story short...here is a summary for anyone who wants to use MySQLdb(Python MySQL driver) on windows.

I installed Python 2.7.1 on Windows which is a simple install from python.org. I installed MySQL Server 5.1 which is a simple msi file install.I then hit some snags with getting MySQLdb to work with Python. So this tutorial is truly for these versions.

  1. Install Python 2.7.1. I installed into "C:\Python27". Make sure you set yout "PATH" environment variable and compile Python with a simple "hello world!" program.
  2. Install MySQL Server5.x. Again make sure that MySQL can be accessed via command line by adding the MySQL Server bin folder to the "PATH" variable
  3. Do not download and compile the MySQL Python Drivers. You will waste time. Instead get precompiled executables from : http://www.lfd.uci.edu/~gohlke/pythonlibs/
  4. Double click on the exe after download and choose the python folder which was "C:\Python27" in my case. This will install the MySQLdb connector
  5. Start Python IDLE(GUI editor) and type "import MySQLdb". If you dont see any errors, you are ready to use the MySQL connector.
It is important to have the Path variables set correctly which makes the big difference. I spent couple of hours on this with missing libraries and changes in site.cfg and found this as the quickest way and hope my fellow coders dont waste time on trivial stuff like these.

Post a Comment