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
>>>
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.
Pingback: Building MySQLdb for Python on Windows « Rational Pie
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!
THANK YOU!!!! It is great!!!!
Cheers from NYC,
Vladimir Garcia
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?
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
thank you very much!!!
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.
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
Thank you! Worked perfectly!
Thank you, this was giving me a headache!
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!
Pingback: Tweets that mention Install MySQL for Python (MySQLdb) on Windows -- Topsy.com
Amazing Tutorial.. Worked like a charm.. Great Job..Thanks a ton..
The above URL to the installer worked best for me. Thanks for the blog post though, thought I was going mad there.
Yun,
Thank you very much for posting this. I just installed this on Windows7 and so far it seems to be working.
Lisa
Actually I found this executable that easy install the mysql.
http://soemin.googlecode.com/files/MySQL-python-1.2.3c1.win32-py2.6.exe
I was having the same issues and was about to download the compiler. This saved my time!
Check it up!
Thx a lot !
it work very well for me.
Pingback: Instalar MySQL para Python (MySQLdb) em Windows « Blog do Victor Jabur
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
Pingback: » Ставим и запускаем Django+MySQL на Windows 7
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?
Awesome, worked perfect – you da man!
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
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?
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
??
Great Post for my website
Thanks! you help me a lot.
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!
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?
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)
Thanks for providing this info, Yun. It was really helpful.
Regards,
Seetharamu
Thanks for providing this info, Yun.
It was really helpful.
Regards,
Seetharamu
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?