Installing MySQLdb for Python 3 in Windows




My favorite Python connected for MySQL or MariaDB is MySQLdb, the problem with this connector is that it is complicated to install on Windows!

I am creating this article for those who want to install MySQLdb for Python 3 for Windows. Especially me, since each time I am doing a Python project that needs to connect to MariaDB or MySQL I always look on how to install MySQLdb.

If you are interested why I prefer MySQLdb compared to other MySQL connectors you may want to read the comparison of MySQL-connector and MySQLdb from Charles Nagy.

Problems with installing MySQLdb on Windows

You can actually install MySQLdb using pip. See pypi documentation here.

Unfortunately, the pypi documentation is already out of date with the latest release was on Jan 3, 2014.

You can still run the command above but it will look for Microsoft Visual C++ Build Tools, which if you install it in your Windows machine for the purpose of only using MySQLdb will become a program that occupies space but you will never ever use again.

Running pip install MySQL-python

So how do we install MySQLdb for Python on Windows? You can follow the steps below.

Installing MySQLdb on Windows

To install MySQLdb on Windows go to this link – https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient

Screenshot of list of precompiled MySQLdb connectors for Python. You can even see when the .whl file was compiled if you hover the mouse over it.

Download the appropriate .whl for your Python version.

I am currently using Python 3.6.8, so I downloaded mysqlclient‑1.3.13‑cp36‑cp36m‑win_amd64.whl.




On windows command prompt, install the .whl file using pip. – pip install [.whl filename]

In my case the command is

Note: The file on the command will be different if you use a different Python version or if there is an update on the version of the mysqlclient (MySQLdb).

Once pip says that you have Successfully installed mysqlclient you are good to go on using MySQLdb as your MySQL connector for Python.

Testing MySQLdb Installation

Below is the code I use if my MySQLdb connector has been properly installed in Windows.

You can change the following parameters to check if it can connect to your MySQL database or MariaDB database.

  • DB_HOST
  • DB_NAME
  • DB_USERNAME
  • DB_PASSWORD

If all is well, then when you run the code above it would print Succesfully Connected to database using MySQLdb!

If MySQLdb is not yet installed then it will raise a ModuleNotFoundError like the screenshot below.

I hope this helped you install MySQLdb for Python on your Windows computer easier.

If there are any questions, errors or suggestions comment them down below so that I could check them out too.




Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.