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
>>>
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.
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?
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
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
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
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
Lots of people suggest compare pluses about become a freelance writer right before ordering custom essay papers in the writing corporation. And we will as get the fantastic texts connecting with this good post right there.
found this: http://www.technicalbard.com/files/MySQL-python-1.2.2.win32-py2.6.exe
installation works, haven’t tested the lib yet.
thanks alot…it worked fine…
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.
@inhahe many thanks for the link, worked like charm!
Thank you very much, worked well with mysql 5.5.
Thanks! Saved my butt
This really saved my bacon!
I also had issues with an older version of the C++ compiler, that was fixed by installing Visual C++ 2008
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.
Thanks, followed your instructions and it did the trick
check this…….
http://www.technicalbard.com/files/MySQL-python-1.2.2.win32-py2.6.exe
Words can barely express my gratitude for your posting. Without your help I would probably have given up after the first error message. Many thanks!
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!