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. Thank you Yun Fu. I found a different set of errors and wanted to post them. I’m running MySQL 5.0 and didn’t have to customize site.cfg in the same way. I have Visual Studion 9.0 installed and tried the build from cygwin. Here is the result. See the unresolved symbols:

    $ python setup.py build
    running build
    running build_py
    copying MySQLdb\release.py -> build\lib.win32-2.6\MySQLdb
    running build_ext
    building ‘_mysql’ extension
    creating build\temp.win32-2.6
    creating build\temp.win32-2.6\Release
    C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -Dversion_info=(1,2,3,’gamma’,1) -D_
    _version__=1.2.3c1 “-IC:\Program Files\MySQL\MySQL Server 5.0\include” -Ic:\Python26\include -Ic:\Python26\PC /Tc_mysql.c /Fobuild\t
    emp.win32-2.6\Release\_mysql.obj /Zl
    _mysql.c
    C:\Program Files\MySQL\MySQL Server 5.0\include\config-win.h(163) : warning C4005: ‘SIZEOF_OFF_T’ : macro redefinition
    c:\python26\include\pyconfig.h(355) : see previous definition of ‘SIZEOF_OFF_T’
    C:\Program Files\MySQL\MySQL Server 5.0\include\config-win.h(313) : warning C4005: ‘HAVE_STDDEF_H’ : macro redefinition
    c:\python26\include\pyconfig.h(641) : see previous definition of ‘HAVE_STDDEF_H’
    C:\Program Files\MySQL\MySQL Server 5.0\include\config-win.h(321) : warning C4005: ‘HAVE_ISNAN’ : macro redefinition
    c:\python26\include\pyconfig.h(406) : see previous definition of ‘HAVE_ISNAN’
    _mysql.c(1363) : warning C4018: ‘
    F:\dev\downloads\MySQL-python-1.2.3c1\MySQL-python-1.2.3c1>python setup.py build

    running build
    running build_py
    copying MySQLdb\release.py -> build\lib.win32-2.5\MySQLdb
    running build_ext
    error: Python was built with Visual Studio 2003;
    extensions must be built with a compiler than can generate compatible binaries.
    Visual Studio 2003 was not found on this system. If you have Cygwin installed,
    you can try compiling with MingW32, by passing “-c mingw32″ to setup.py.

    And of course, both cygwin gcc and mingw32 gcc builds didn’t work because of multiple definitions and undefined references.

    Did you build python2.6 yourself with Visual Studio 9?

  2. Hi Jonathan,

    I did not build python 2.6 by myself. I installed it directly using the installer for windows. It is pythong 2.6 Windows version, not cygwin. I can see it is compiled by MSC v.1500 32 bit on win32.

    thanks!

    yun

  3. Hi
    I’m trying to the same procedures but with a difference in python (I’m using python 2.4.4)
    I’m getting this error :
    copying MySQLdb\release.py -> build\lib.win32-2.4\MySQLdb
    running build_ext
    building ‘_mysql’ extension
    gcc -mno-cygwin -mdll -O -Wall -Dversion_info=(1,2,3,’gamma’,1) -D__version__=1.2.3c1 “-IC:\opt\MySQL\MySQL Server 5.1\include” -IC:\opt\Python24\include -IC:\o
    pt\Python24\PC -c _mysql.c -o build\temp.win32-2.4\Release\_mysql.o /Zl
    error: command ‘gcc’ failed: No such file or directory

    I removed MinGW from my path and added Visual C++ path

    I don’t know where it is picking gcc from

    Should I use some clause in setup.cfg file under [build_ext] item ?

    Thanks

  4. Hi Erico,

    Sorry, I don’t know why it picks gcc in your case. Maybe MinGW left some environment variables. Maybe you should try to search for how to customize distutils to set c compiler. For me, if I do this, I can see it automatically picks MSVC

    >>> from distutils.ccompiler import new_compiler
    >>> c = new_compiler(None, None, None, None)
    >>> c
    < distutils.msvc9compiler.MSVCCompiler instance at 0x00BC25A8 >

    thanks!

    Yun

  5. Thanks Yun

    the problem was that I had configured distutils for plone3 and it had mingw32 referenced

    now I got the same error from Jonathan.:
    running build_ext
    error: Python was built with Visual Studio 2003;
    extensions must be built with a compiler than can generate compatible binaries
    Visual Studio 2003 was not found on this system. If you have Cygwin installed,
    you can try compiling with MingW32, by passing “-c mingw32″ to setup.py.

    I installed python 2.4.4 with msi installer too

    I have added Microsoft Visual Studio 9.0\VC\bin to PATH but it seems that it didn’t recognize it

    Regards
    Érico

  6. Pingback: Building MySQLdb for Python on Windows « Rational Pie

  7. Thank you! You saved me hours and hours of work. I would have been searching all day on how to solve these very cryptic errors.

  8. Yun,

    Thank you, thank you, thank you, thank you, thank you! I just can’t thank you enough for posting this. Worked like a charm.

  9. THANK YOU!!!! This has been kicking my ass all day. I’ve tried about 3 different solutions to fix the issue, but yours was the only one that worked.

    If you ever come to Kansas City, drinks are on me!

  10. Yun,

    By mistake I have followed all your steps but without the Microsoft Visual C++ 2008 Express Edition installed…

    I went into some of your errors, but at the end the “import MySQLdb” did not work!

    So I have download and installed the Microsoft Visual C++ 2008 Express Edition and tried to build the setup.py again…

    It failed baddly! but I’m not too sure how too proceed now. Any hint?

  11. Hi Luiz,

    Sorry to hear that. I just dumped all steps I have done there.
    I can imagine If anything does not match exactly, it may fail.
    I am sorry that I have no idea what’s the reason for your problem.

    Sorry!

    Yun

  12. Thank you for this walk through it is really great.

    I also need to compile the pyd file in debug mode. Do you know what steps are required to do this?

    I have been modifying the extra_compile_args in setup_windows.py. to
    extra_compile_args = [ '/Zl', '/LDb' ]

    but the compiled pyd is the same size. So it seems nothing happened.

  13. There is a –debug option which can be specified when building.

    After setting this I had to add the path to my own compiled python binaries in setup_windows.cfg to library_dirs

  14. Many thanks for posting this, Yun. I finally got pylzma to build on Windows XP under Python 2.6.

    First, it was failing to find vcvarsall.bat even though I had just installed Visual Studio 10 and even after I made sure vcvarsall was in the PATH. I grepped for the error message, using TextPad and found msvc9compiler.py. Examining that file, I found it was looking for environment variable VS90COMNTOOLS. VS 10 had already added VS100COMNTOOLS, so I just copied its value to VS90COMNTOOLS.

    Then I got the manifest building error, found your post, installed your fix, and it worked!

  15. Pingback: Tweets that mention Install MySQL for Python (MySQLdb) on Windows -- Topsy.com

  16. The above URL to the installer worked best for me. Thanks for the blog post though, thought I was going mad there.

  17. Yun,
    Thank you very much for posting this. I just installed this on Windows7 and so far it seems to be working.
    Lisa

  18. Pingback: Instalar MySQL para Python (MySQLdb) em Windows « Blog do Victor Jabur

  19. Hi Yun,

    Thanks for all of your efforts. I tried all of your steps and tools except the MS Visual C++ 2008 Express edition, rather I have MS Visual Studio 2005. I got the following error:

    C:\software\MySQL_FROM_Python\MySQL-python-1.2.3c1\MySQL-python-1.2.3c1>setup.py
    build
    running build
    running build_py
    creating build
    creating build\lib.win32-2.6
    copying _mysql_exceptions.py -> build\lib.win32-2.6
    creating build\lib.win32-2.6\MySQLdb
    copying MySQLdb\__init__.py -> build\lib.win32-2.6\MySQLdb
    copying MySQLdb\converters.py -> build\lib.win32-2.6\MySQLdb
    copying MySQLdb\connections.py -> build\lib.win32-2.6\MySQLdb
    copying MySQLdb\cursors.py -> build\lib.win32-2.6\MySQLdb
    copying MySQLdb\release.py -> build\lib.win32-2.6\MySQLdb
    copying MySQLdb\times.py -> build\lib.win32-2.6\MySQLdb
    creating build\lib.win32-2.6\MySQLdb\constants
    copying MySQLdb\constants\__init__.py -> build\lib.win32-2.6\MySQLdb\constants
    copying MySQLdb\constants\CR.py -> build\lib.win32-2.6\MySQLdb\constants
    copying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win32-2.6\MySQLdb\constants

    copying MySQLdb\constants\ER.py -> build\lib.win32-2.6\MySQLdb\constants
    copying MySQLdb\constants\FLAG.py -> build\lib.win32-2.6\MySQLdb\constants
    copying MySQLdb\constants\REFRESH.py -> build\lib.win32-2.6\MySQLdb\constants
    copying MySQLdb\constants\CLIENT.py -> build\lib.win32-2.6\MySQLdb\constants
    running build_ext
    building ‘_mysql’ extension
    error: Unable to find vcvarsall.bat

    I’d really appreciate if you can help me to resolve the issue.

    Thanks,
    Sanjoy

  20. Pingback: » Ставим и запускаем Django+MySQL на Windows 7

  21. Hello Yun,

    Thank you, I managed to download setuptools and it started to build… untill that message:

    “”"error: Python was built with Visual Studio version 7.1, and extensions need to be built with the same version of the compiler, but it isn’t installed.”"”

    What should I do whith that? I installed Python 2.5 with the installer. I didn’t even know that it was build with Visual Studio 7.1! Should I add it in the path?

  22. Hi.. My mysql database is located on a remote machine… So i dont have any local copy of mysql on my local machine.. i get the registry key error… (file not found)… But following your instructions, it requires to have a local copy of mysql… How do i install the mysqldb for database residing on another machine??? Please help and thanks in advance

  23. Hello,

    Thank you for your post.
    I’m trying to install mysqldb with python 2.5, downloaded directly from sourceforge… So that means that it was precompiled (by Visual Studio 7.1, ie 2003).
    So, when I try to do: “python setup.py install”, it says to me :
    “”"building ‘_mysql’ extension
    error: Python was built with Visual Studio version 7.1, and extensions need to be built with the same version of the compiler, but it isn’t installed.”"”

    But what I don’t understand is that I have VS 2003 installed on my computer. I added it in my path… but I can’t go on the installation …
    Any idea of what’s wrong?

  24. Hi,

    Thank very much. but is this message ok(i can ignore it ?):

    D:\server\python26\lib\site-packages\mysql_python-1.2.3-py2.6-win32.egg\_mysql.p
    y:3: UserWarning: Module _mysql was already imported from D:\server\python26\lib
    \site-packages\mysql_python-1.2.3-py2.6-win32.egg\_mysql.pyc, but d:\mysql-pytho
    n-1.2.3 is being added to sys.path

    ??

  25. I LOVE You so much!
    Thanks! I Spent a whole day trying to figure out what the heck was going on! I can hardly believe it finally works!

  26. Thanks for the last suggestion. I went all the way up to the manifest thing, and was too tired to investigate.. But this manifest switch is just a bug in the msvc9compiler class or is something more specific to the MySQL module? That is, should I leave the switch there?

  27. Thanks for the last suggestion. I went all the way up to the manifest thing, and was too tired to investigate further. But this missing manifest switch is just a bug in msvc9compiler or is something related to certain modules? Should I leave it there?
    (n.b. if this gets double-posted, sorry… blame my company firewall)

  28. For some reason, this CAPTCHA is rejecting me…. I’ve tried about 10 times to post this comment, and it keeps saying
    “That reCAPTCHA response was incorrect” or”duplicate comment.)
    —–
    Thanks very much for the suggestion. Saved some hours of work.
    This /manifest switch, is it just a bug in msvc9compiler.py or is something that is required only occasionally?