Question or problem about Python programming:

Mac OS needs python. DO NOT remove any versions of Python found in the following folders: /usr/bin; system/Library; These versions of Python—which should be Python 2.7—are installed by Apple and used by Mac OS and other software to perform some functions. Prerequisites for installing Python3 on Mac Install Xcode. Xcode is Apple's Integrated Development Environment (IDE). You might already have Xcode on your Mac. Use Python 3 as the macOS default Python's website has a macOS Python 3 installer we can download and use. If we use the package installation, a python3 fill will be available in /usr/local/bin/. Aliasing is a must since the Python binary stored in /usr/bin/ can't be changed. Installing Packages¶. This section covers the basics of how to install Python packages. It’s important to note that the term “package” in this context is being used to describe a bundle of software to be installed (i.e. As a synonym for a distribution).

I’m a Python newbie, but I’ve just spent a day working out how to get MySQLdb working properly, and the universe according to google includes numerous references to what a PITA it is, and an inordinate number of guides that seem to be outdated. Given that this site is intended to address these sorts of problems, and I know that I’m going to need a reference to the solution in future, I’m going to ask the question, provide my answer and see what else floats to the surface.

So the question is how to get MySQLdb working on Mac OS X?

How to solve the problem:

Solution 1:

Update for those using Python3:
You can simply use conda install mysqlclient to install the libraries required to use MySQLdb as it currently exists. The following SO question was a helpful clue: Python 3 ImportError: No module named ‘ConfigParser’ . Installing mysqlclient will install mysqlclient, mysql-connector, and llvmdev (at least, it installed these 3 libraries on my machine).

Here is the tale of my rambling experience with this problem. Would love to see it edited or generalised if you have better experience of the issue… apply a bit of that SO magic.

Note: Comments in next paragraph applied to Snow Leopard, but not to Lion, which appears to require 64-bit MySQL

First off, the author (still?) of MySQLdb says here that one of the most pernicious problems is that OS X comes installed with a 32 bit version of Python, but most average joes (myself included) probably jump to install the 64 bit version of MySQL. Bad move… remove the 64 bit version if you have installed it (instructions on this fiddly task are available on SO here), then download and install the 32 bit version (package here)

There are numerous step-by-steps on how to build and install the MySQLdb libraries. They often have subtle differences. This seemed the most popular to me, and provided the working solution. I’ve reproduced it with a couple of edits below

Step 0:
Before I start, I assume that you have MySQL, Python, and GCC installed on the mac.

Step 1:
Download the latest MySQL for Python adapter from SourceForge.

Step 2:
Extract your downloaded package:

Step 3:
Inside the folder, clean the package:

COUPLE OF EXTRA STEPS, (from this comment)

Step 3b:
Remove everything under your MySQL-python-1.2.2/build/* directory — don’t trust the “python setup.py clean” to do it for you

Install

Step 3c:
Remove the egg under Users/$USER/.python-eggs

Step 4:
Originally required editing _mysql.c, but is now NO LONGER NECESSARY. MySQLdb community seem to have fixed this bug now.

Step 5:
Create a symbolic link under lib to point to a sub-directory called mysql. This is where it looks for during compilation.

Step 6:
Edit the setup_posix.py and change the following

mysql_config.path = “mysql_config”

to

mysql_config.path = “/usr/local/mysql/bin/mysql_config”

Step 7:
In the same directory, rebuild your package (ignore the warnings that comes with it)

Step 8:
Install the package and you are done.

Step 9:
Test if it’s working. It works if you can import MySQLdb.

>>> import MySQLdb

Step 10:
If upon trying to import you receive an error complaining that Library not loaded: libmysqlclient.18.dylib ending with: Reason: image not found you need to create one additional symlink which is:

You should then be able to import MySQLdb without any errors.

One final hiccup though is that if you start Python from the build directory you will get this error:

/Library/Python/2.5/site-packages/MySQL_python-1.2.3c1-py2.5-macosx-10.5-i386.egg/_mysql.py:3: UserWarning: Module _mysql was already imported from /Library/Python/2.5/site-packages/MySQL_python-1.2.3c1-py2.5-macosx-10.5-i386.egg/_mysql.pyc, but XXXX/MySQL-python-1.2.3c1 is being added to sys.path

This is pretty easy to Google, but to save you the trouble you will end up here (or maybe not… not a particularly future-proof URL) and figure out that you need to cd . out of build directory and the error should disappear.

As I wrote at the top, I’d love to see this answer generalised, as there are numerous other specific experiences of this horrible problem out there. Edit away, or provide your own, better answer.

Solution 2:

A quick and easy way for Mac OS X 10.8 (Mountain Lion), 10.9 (Mavericks), 10.10 (Yosemite), and 10.11 (El Capitan):

I assume you have XCode, its command line tools, Python, and MySQL installed.

  1. Install PIP:

  2. Edit ~/.profile: (Might not be necessary in Mac OS X 10.10)

    Copy and paste the following two line

    Save and exit. Afterwords execute the following command:

  3. Install MySQLdb

    To test if everything works fine just try

It worked like a charm for me. I hope it helps.

If you encounter an error regarding a missing library: Library not loaded: libmysqlclient.18.dylib then you have to symlink it to /usr/lib like so:

Solution 3:

Install mysql and python via Macports The porters have done all the difficult work.

Install Python Library Mac Download

should install what you need. (see Stack overflow for comments re mysql server)

If you only need to connect to mysql and not run a server then the first line is sufficient.

Macports now (early 2013) will provide binary downloads for common combinations of OS a executable architecture, for others (and if you request it) it will build from source.

In general macports (or fink) help when there are complex libraries etc that need to be installed.

Download reloader activator kuyhaa. Python only code and if simple C dependencies can be set up via setuptools etc, but it begins to get complex if you mix the two.

Solution 4:

Install pip:

Install brew:

Install mysql:

Install MySQLdb

If you have compilation problems, try editing the ~/.profile file like in one of the answers here.

Solution 5:

Here’s another step I had to go through, after receiving an error on completing Step 9:

Reference: Thanks! http://ageekstory.blogspot.com/2011_04_01_archive.html

Python Library Download

Hope this helps!