Integrating MySQL with Python in Windows

0 votes
asked Mar 14, 2009 by sammy

I am finding it difficult to use MySQL with Python in my windows system.

I am currently using Python 2.6. I have tried to compile MySQL-python-1.2.3b1 (which is supposed to work for Python 2.6 ?) source code using the provided setup scripts. The setup script runs and it doesn't report any error but it doesn't generate _mysql module.

I have also tried setting up MySQL for Python 2.5 with out success. The problem with using 2.5 is that Python 2.5 is compiled with visual studio 2003 (I installed it using the provided binaries). I have visual studio 2005 on my windows system. Hence setuptools fails to generate _mysql module.

Any help ?

14 Answers

0 votes
answered Mar 11, 2009 by itsadok

Download page for python-mysqldb. The page includes binaries for 32 and 64 bit versions of for Python 2.5, 2.6 and 2.7.

There's also discussion on getting rid of the deprecation warning.

UPDATE: This is an old answer. Currently, I would recommend using PyMySQL. It's pure python, so it supports all OSes equally, it's almost a drop-in replacement for mysqldb, and it also works with python 3. The best way to install it is using pip. You can install it from here (more instructions here), and then run:

pip install pymysql
0 votes
answered Mar 14, 2009 by mork0075

You're not the only person having problems with Python 2.6 and MySQL ( Here's an explanation how it should run under Python 2.5 Good luck

0 votes
answered Mar 14, 2009 by dave-webb
0 votes
answered Mar 15, 2009 by lfaraone

You might want to also consider making use of Cygwin, it has mysql python libraries in the repository.

0 votes
answered Mar 13, 2010 by gorgonzola-balistico

This may read like your grandpa givin advice, but all answers here did not mention the best way: go nd install ActivePython instead of windows binaries. I was really wondering for a long time why Python development on windows was such a pita - until I installed activestate python. I am not affiliated with them. It is just the plain truth. Write it on every wall: Python development on Windows = ActiveState! you then just pypm install mysql-python and everything works smoothly. no compile orgy. no strange errors. no terror. Just start coding and doing real work after five minutes. This is the only way to go on windows. Really.

0 votes
answered Mar 29, 2010 by jasonfruit

What about pymysql? It's pure Python, and I've used it on Windows with considerable success, bypassing the difficulties of compiling and installing mysql-python.

0 votes
answered Mar 11, 2011 by fijiaaron

You can also use pyodbc with the MySQL Connector/ODBC to use MySQL on Windows. Unixodbc is also available to make the code compatible on Linux. Pyodbc uses the standard Python DB API 2.0 so if you stick with that switching between MySQL/PostgreSQL/SQLite/ODBC/JDBC drivers etc. should be relatively painless.

0 votes
answered Mar 14, 2011 by evan

Because I am running python in a (pylons/pyramid) virtualenv, I could not run the binary installers (helpfully) linked to previously.

I had problems following the steps with Willie's answer, but I determined that the problem is (probably) that I am running windows 7 x64 install, which puts the registry key for mysql in a slightly different location, specifically in my case (note: I am running version 5.5) in: "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\MySQL AB\MySQL Server 5.5".

HOWEVER, "HKEY_LOCAL_MACHINE\" cannot be included in the path or it will fail.

Also, I had to do a restart between steps 3 and 4.

After working through all of this, IMO it would have been smarter to run the entire python dev environment from cygwin.

0 votes
answered Mar 17, 2011 by gustavo-vargas

You can try to use myPySQL. It's really easy to use; no compilation for windows, and even if you need to compile it for any reason, you only need Python and Visual C installed (not mysql).

Good luck

0 votes
answered Mar 2, 2012 by eran-medan

If you are looking for Python 3.2 this seems the best solution I found so far


Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter