Integrating MySQL with Python in Windows

110,333

Solution 1

Download page for python-mysqldb. The page includes binaries for 32 and 64 bit versions of for Python 2.5, 2.6 and 2.7.

There's also discussion on getting rid of the deprecation warning.

UPDATE: This is an old answer. Currently, I would recommend using PyMySQL. It's pure python, so it supports all OSes equally, it's almost a drop-in replacement for mysqldb, and it also works with python 3. The best way to install it is using pip. You can install it from here (more instructions here), and then run:

pip install pymysql

Solution 2

This may read like your grandpa givin advice, but all answers here did not mention the best way: go nd install ActivePython instead of python.org windows binaries. I was really wondering for a long time why Python development on windows was such a pita - until I installed activestate python. I am not affiliated with them. It is just the plain truth. Write it on every wall: Python development on Windows = ActiveState! you then just pypm install mysql-python and everything works smoothly. no compile orgy. no strange errors. no terror. Just start coding and doing real work after five minutes. This is the only way to go on windows. Really.

Solution 3

As Python newbie learning the Python ecosystem I've just completed this.

  1. Install setuptools instructions

  2. Install MySQL 5.1. Download the 97.6MB MSI from here You can't use the essentials version because it doesnt contain the C libraries.
    Be sure to select a custom install, and mark the development tools / libraries for installation as that is not done by default. This is needed to get the C header files.
    You can verify you have done this correctly by looking in your install directory for a folder named "include". E.G C:\Program Files\MySQL\MySQL Server 5.1\include. It should have a whole bunch of .h files.

  3. Install Microsoft Visual Studio C++ Express 2008 from here This is needed to get a C compiler.

  4. Open up a command line as administrator (right click on the Cmd shortcut and then "run as administrator". Be sure to open a fresh window after you have installed those things or your path won't be updated and the install will still fail.

  5. From the command prompt:

    easy_install -b C:\temp\sometempdir mysql-python

    That will fail - which is OK.

    Now open site.cfg in your temp directory C:\temp\sometempdir and edit the "registry_key" setting to:

    registry_key = SOFTWARE\MySQL AB\MySQL Server 5.1

    now CD into your temp dir and:

    python setup.py clean

    python setup.py install

    You should be ready to rock!

  6. Here is a super simple script to start off learning the Python DB API for you - if you need it.

Solution 4

I found a location were one person had successfully built mysql for python2.6, sharing the link, http://www.technicalbard.com/files/MySQL-python-1.2.2.win32-py2.6.exe

...you might see a warning while import MySQLdb which is fine and that won’t hurt anything,

C:\Python26\lib\site-packages\MySQLdb__init__.py:34: DeprecationWarning: the sets module is deprecated from sets import ImmutableSet

Solution 5

What about pymysql? It's pure Python, and I've used it on Windows with considerable success, bypassing the difficulties of compiling and installing mysql-python.

Share:
110,333

Related videos on Youtube

Sammy
Author by

Sammy

Updated on September 12, 2020

Comments

  • Sammy
    Sammy over 3 years

    I am finding it difficult to use MySQL with Python in my windows system.

    I am currently using Python 2.6. I have tried to compile MySQL-python-1.2.3b1 (which is supposed to work for Python 2.6 ?) source code using the provided setup scripts. The setup script runs and it doesn't report any error but it doesn't generate _mysql module.

    I have also tried setting up MySQL for Python 2.5 with out success. The problem with using 2.5 is that Python 2.5 is compiled with visual studio 2003 (I installed it using the provided binaries). I have visual studio 2005 on my windows system. Hence setuptools fails to generate _mysql module.

    Any help ?

    • Eran Medan
      Eran Medan almost 12 years
      Since 3 years have passed since this was asked, and 2 years since last high voted answer, see updated answer for Python 3.2 below (or see wiki.python.org/moin/MySQL)
  • David Sykes
    David Sykes almost 15 years
    The 2.5 (and 2.4) binaries fail to install if only 2.6 is installed
  • John C
    John C over 13 years
    Can anyone confirm that the first linked executable works with Python 2.6? The installation splash screen says it's only 2.5 - but I'm presuming the person who built it may just have not updated that line of documentation.
  • Matheus Moreira
    Matheus Moreira over 13 years
    It would be a perfect solution if a Business Edition subscription wasn't necessary...
  • Sridhar Ratnakumar
    Sridhar Ratnakumar over 13 years
    @matheus.emm - Business Edition is not necessary for installing MySQLdb on Windows 32-bit.
  • Matheus Moreira
    Matheus Moreira over 13 years
    @Sridhar What do you mean? When I tried to execute the command to install mysql-python a message was displayed saying that a Business Edition subscription was necessary. Is the a (legal) way to install mysql-python without this subscription on Windows 7 64 bits?
  • Sridhar Ratnakumar
    Sridhar Ratnakumar over 13 years
    @matheus.emm - For free access, you can install ActivePython 32-bit on your Windows 7 64-bit OS and then install mysql-python. ActivePython 64-bit does require a BE subscription for installing packages.
  • TWA
    TWA over 13 years
    This is great advice. I think the Business Edition is well worth it if you are seriously using Python in production. If it saves you a few hours of work a month it pays for itself. All of these configuration headaches make me remember the open source business model: "Give them the software for free, but make it so hard to install and use they need to buy the manual and/or support." Only problem is many of these projects aren't providing or selling support because they see open source as an ideology instead of what it was intended to be, a business model. lolz
  • Cleber Goncalves
    Cleber Goncalves over 13 years
    Just installed it using activepython community edition and it worked like a charm. Highly recommended
  • Vilva
    Vilva almost 12 years
    I installed python 2.7 x64 but while importing MySQLdb I'm getting 'ImportError: No module named _mysql_windows.api' error. pls anyone help me to solve this issue.
  • Shailen
    Shailen over 10 years
    Well after doing pip install pymysql, syncdb still complained about the missing MySQLdb module.
  • Simon Wang
    Simon Wang over 10 years
    Great!Work for ActivePython on Win7!
  • hizbul25
    hizbul25 about 10 years
    while i run this command in my windows 8.1 64 bit system it shows: Traceback (most recent call last): File "C:\Python33\Scripts\pip-script.py", line 5, in <module> from pkg_resources import load_entry_point ImportError: No module named 'pkg_resources'
  • itsadok
    itsadok about 10 years
    @shailenTJ (and anyone with a similar problem) look here
  • itsadok
    itsadok about 10 years
    @hizbul25 pip installation is much simpler these days. Download get-pip.py from here and run it. More info here
  • Phillip
    Phillip over 6 years
    If anyone may be able to help, I'm having trouble using pymysql: stackoverflow.com/questions/46145845/…