Install MySQL for Python (MySQLdb) on Windows

It took me quite a while to figure out how to build and install MySQL for Python (MySQLdb) on Windows. I’d better write it down.

There is no binary distribution of MySQLdb for Python 2.6 on Windows. I have to build it from the source. My environment is Windows XP. MySQL 5.1. Python 2.6 (windows version, not cygwin), and MySQL-python-1.2.3c1. Also, I have Microsoft Visual C++ 2008 Express Edition (Microsoft Visual Studio 9.0) installed, which is required to compile the C code in MySQL-python.

First of all, install Python setuptools, if you haven’t installed it. It is required in MySQL-python setup.py. I also added C:\Python26\Scripts into environment PATH, where easy_install is installed.

Then, make sure you have MySQL Developer Components installed. Download MySQL msi installer version, select “Developer Components” in Custom Setup. It will install C:\Program Files\MySQL\MySQL Server 5.1\include, lib\debug and lib\opt for you. They are not installed by default.

Uncompress MySQL-python-1.2.3c1.tar.gz into a directory. Open a command window (cmd), change to the directory.

Try to run,

setup.py build

I got this error in setup_windows.py:

in get_config
serverKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, options['registry_key'])
WindowsError: [Error 2] The system cannot find the file specified

So I edited site.cfg, changed the MySQL version from 5.0 to 5.1 (since I am using 5.1)

registry_key = SOFTWARE\MySQL AB\MySQL Server 5.1

You can use regedit to check which version you are using. It is specified at: HKEY_LOCAL_MACHINE/SOFTWARE/MySQL AB/MySQL Server 5.1.

Now try to build it again. I got this error:

build\temp.win32-2.6\Release\_mysql.pyd.manifest : general error c1010070: Failed to load and parse the manifest. The system cannot find  the file specified.
error: command ‘mt.exe’ failed with exit status 31

To fix this problem, go to C:\Python26\Lib\distutils, edit msvc9compiler.py, search for ‘MANIFESTFILE’, you will find the following line

ld_args.append(‘/MANIFESTFILE:’ + temp_manifest)

Then append the following line after the above line,

ld_args.append(‘/MANIFEST’)

Then go back to run “setup.py build”, it will succeed. Finally, run

setup.py install

Test it in python

>>> import MySQLdb
>>>

104 thoughts on “Install MySQL for Python (MySQLdb) on Windows

  1. C:\Python23\MySQL-python-1.2.3>setup.py build
    running build
    running build_py
    copying MySQLdb\release.py -> build\lib.win32-2.3\MySQLdb
    running build_ext
    building ‘_mysql’ extension
    C:\Program Files\Microsoft Visual Studio\VC98\BIN\link.exe /DLL /nologo /INCREME
    NTAL:NO “/LIBPATH:C:\Program Files\MySQL\MySQL Server 5.5\lib\opt” /LIBPATH:C:\P
    ython23\libs /LIBPATH:C:\Python23\PCBuild kernel32.lib advapi32.lib wsock32.lib
    mysqlclient.lib /EXPORT:init_mysql build\temp.win32-2.3\Release\_mysql.obj /OUT:
    build\lib.win32-2.3\_mysql.pyd /IMPLIB:build\temp.win32-2.3\Release\_mysql.lib
    LINK : fatal error LNK1181: cannot open input file “mysqlclient.lib”
    error: command ‘”C:\Program Files\Microsoft Visual Studio\VC98\BIN\link.exe”‘ fa
    iled with exit status 1181

    I cannot figure out the reason for the error. I have got Microsoft Visual Studio 6, Python 2.3 and MySQL 5.5 installed in my system. I am trying to load MySQLdb

  2. Hey ,

    I followed your instructions but it does’nt help. First of all in windows 7 the SQL gets installed under Wow6432Node registry key. So i changed it in site.cfg but now there comes no error but am unable to import MySQLdb……

  3. After two days trying to get this working I found the following steps seemed to work for me – read these in conjunction with the packaged README.

    For Windows:
    - download and install the following:
    1. Python 2.7 http://www.python.org/
    2. Setuptools http://pypi.python.org/pypi/setuptools
    3. MinGW http://www.mingw.org/category/wiki/download
    4. Mysql 5.1 http://www.mysql.com/downloads/

    - Don’t worry about Zlib
    - Don’t worry about OpenSSL

    - comment out these lines in setup_windows.py
    # XXX static doesn’t actually do anything on Windows
    #if enabled(options, ‘embedded’):
    # client = “mysqld”
    #else:
    # client = “mysqlclient”

    - Add this line below the commented lines (this instructs the compiler to use the dynamic version of the mysql libraries – static will only work using the same compiler as mysql was compiled with.
    client = “libmysql”

    - Add the following to the PATH environment variable
    C:\MinGW\bin;

    - Change this line in site.cfg to the following
    registry_key = SOFTWARE\MySQL AB\MySQL Server 5.1

    - Run this command
    python setup.py build –compile=mingw32

    - Then Run this command
    python setup.py install