Home > Python, Tech > Building MySQLdb for Python on Windows

Building MySQLdb for Python on Windows


MySQLdb is perhaps the most widely used interface between python and MySQL. Sadly for the python developers on Windows, there are no official pre-built binaries for MySQLdb for window. You are expected to build for your own system using the scripts provided.

As almost always is the case, this path is fraught with problems. I have documented below the problems I ran into while building MySQLdb myself.

System:

  • Win32 (XP)
  • Microsoft Visual C++ compiler
  • MySQL developer build: to install this make sure that you select the “C headers\libs” option on the installation configuration page (this usually is the first page of the wizard). If you do not have these components installed you will see errors like this: _mysql.c(34) : fatal error C1083: Cannot open include file: ‘config-win.h’: No such file or directory
    error: command ‘”C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\cl.exe”‘ failed with exit
    status 2

  • setuptools.py: get the installer from http://pypi.python.org/pypi/setuptools

Steps to install:

  1. open site.cfg and  look for the line: registry_key = SOFTWARE\MySQL AB\MySQL Server 5.0, update it for your version of MySQL, I changed it to registry_key = SOFTWARE\MySQL AB\MySQL Server 5.1
  2. run python setup.py build
  3. if you get error like this:

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

    As suggested here do the following:

    • go to C:\Program Files\Python26\Lib\distutils or a similar path depending on where your target python version is installed
    • open the file msvc9compiler.py and look for MANIFESTFILE, you should see the following line
      ld_args.append(’/MANIFESTFILE:’ + temp_manifest)
    • Add another line just after this one
      ld_args.append(’/MANIFEST’)
  4. run python setup.py bdist_wininst . Thus will give you an executable in the \dist directory, run this .exe file and get going!
  1. January 27, 2010 at 12:55 am

    thanks for the instructions – they’re very helpful! I’ve encountered 2 of 3 errors you metnioned, but finally I have installed a precompiled version of MySQLdb
    http://www.technicalbard.com/files/MySQL-python-1.2.2.win32-py2.6.exe
    I’ve found it in comments under this blog entry:
    http://www.fuyun.org/2009/12/install-mysql-for-python-on-windows/ (thanks inhahe)

  2. xms
    March 9, 2011 at 9:31 am

    Thanks – a couple additions. If you are running in Win 64 bit OS using Python x64 install (which kinda makes sense) you run into additional issues using setuptools.py since it doesn’t know to use Win64 registry. I found it easiest to just uninstall x64 Python and install x32 Python.
    A similar issue applies to your site.cfg step – change to reflect 64 bit registry:
    registry_key = SOFTWARE\Wow6432Node\MySQL AB\MySQL Server 5.x
    Also MySQL 5.5 didn’t seem to have the headers so installed 5.1, which was still available on MySQL site.

  3. Carlo
    April 29, 2011 at 1:49 pm

    Hi there!

    I just compiled MySQLdb for Windows 64bit and came to another point that might happen to someone else. I had to compile the MySQLdb without having MySQL installed and without administrator rights on my machine. So there is a way to just download the MySql Connector/C libraries that you can find here: http://dev.mysql.com/downloads/connector/c/
    Once this is downloaded and unpacked I had to modify the setup_windows.py file of the mysqldb sources:

    #serverKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, options['registry_key'])
    #mysql_root, dummy = _winreg.QueryValueEx(serverKey,'Location')
    mysql_root = r"C:\PATH_TO_MY\mysql-connector-c-noinstall-6.0.2-winx64"

    And following line, too:

    library_dirs = [ os.path.join(mysql_root, r'lib') ]

    Instead of:

    library_dirs = [ os.path.join(mysql_root, r'lib\opt') ]

    And this worked out pretty well with all the explanations that you give above.

  4. April 3, 2016 at 8:15 am

    I am new to Python. Could you please tell me where site.cfg is located?

  1. January 17, 2011 at 3:08 pm
  2. June 7, 2011 at 12:59 am

Leave a comment