52

I face this problem when I want to install mysql-python on osx 10.8, it show error with 'my_config.h' file not found.

Below is my running code:

sudo easy_install mysql-python
Password:
Searching for mysql-python
Reading http://pypi.python.org/simple/mysql-python/
Reading http://sourceforge.net/projects/mysql-python/
Reading http://sourceforge.net/projects/mysql-python
Best match: MySQL-python 1.2.3
Downloading http://pypi.python.org/packages/source/M/MySQL-python/MySQL-python-1.2.3.tar.gz#md5=215eddb6d853f6f4be5b4afc4154292f
Processing MySQL-python-1.2.3.tar.gz
Running MySQL-python-1.2.3/setup.py -q bdist_egg --dist-dir /tmp/easy_install-UoZlf7/MySQL-python-1.2.3/egg-dist-tmp-phaQqE
warning: no files found matching 'MANIFEST'
warning: no files found matching 'ChangeLog'
warning: no files found matching 'GPL'
clang: warning: argument unused during compilation: '-mno-fused-madd'
_mysql.c:36:10: fatal error: 'my_config.h' file not found
#include "my_config.h"
         ^
1 error generated.
error: Setup script exited with error: command 'clang' failed with exit status 1
ninjaiimac:MySQL-python-1.2.3 sunninjaisun$ 
Cœur
  • 37,241
  • 25
  • 195
  • 267
ninjaisun
  • 521
  • 1
  • 5
  • 3

14 Answers14

86
brew install mysql
brew unlink mysql
brew install mysql-connector-c
sed -i -e 's/libs="$libs -l "/libs="$libs -lmysqlclient -lssl -lcrypto"/g' /usr/local/bin/mysql_config
pip install MySQL-python
brew unlink mysql-connector-c
brew link --overwrite mysql
Nelson Tatius
  • 7,693
  • 8
  • 47
  • 70
  • 4
    These steps work for me. macOS 10.13.6. python 2.7.15. – jyu Jul 25 '18 at 18:05
  • Works for me too on High Sierra and Python 2.7.15. – Laurent Lasudry Aug 31 '18 at 13:44
  • Thanks!! Works like a charm – Agam Banga Sep 26 '18 at 17:55
  • 2
    This worked for me as is on macOS Mojave (10.14.1) and python 2.7.10. Thanks!! – Silvain Nov 08 '18 at 07:31
  • 13
    Description for this answer should read: "The default mysql package does not ship with the mysql header files, but the mysql-connector-c package does. Problem is, they overwrite the mysql command, so we first have to unlink the default package, then add the mysql-connector-c one, and then relink mysql again. – Emil Stenström Feb 03 '19 at 07:41
  • 5
    I had to change the "pip install" command to "LDFLAGS=-L/usr/local/opt/openssl/lib pip install MySQL-python" for this to work for me on Mojave. – user108471 Mar 07 '19 at 19:31
  • 1
    This doesn't work at all. The MySQL-python package has a string index error. Turns out it's because you have to run the `brew link --overwrite mysql` command before the `pip install` command. – Throw Away Account May 02 '19 at 20:32
  • This worked for me, after I made clang able to find `openssl`, exporting `LDFLAGS=-L/usr/local/opt/openssl/lib`, and `CPPFLAGS=-I/usr/local/opt/openssl/include` – Camilo Díaz Repka Jul 23 '19 at 19:54
  • 10
    Doesn't work at all. It throws the same error on: `pip install MySQL-python` – saran3h Jan 06 '20 at 19:49
  • 2
    mysql-connector-c is not available anymore, it has been replaced with mysql-client. What should I do? – Tu Le Oct 30 '20 at 07:11
  • 1
    when executing to `sed -i -e 's/libs="$libs -l "/libs="$libs -lmysqlclient -lssl -lcrypto"/g' /usr/local/bin/mysql_config` failed to sed: /usr/local/bin/mysql_config: No such file or directory. And my osx version is catalina. – xiaojueguan Nov 16 '20 at 02:38
  • Please read [answer] and [edit] your question to contain an explanation as to why this code would actually solve the problem at hand. Always remember that you're not only solving the problem, but are also educating the OP and any future readers of this post. – Adriaan Jul 18 '22 at 09:28
39

Works well on macOS High Sierra:

If you haven't already installed mysql: brew install mysql

brew unlink mysql
brew install mysql-connector-c
brew link --overwrite mysql
pip install MySQL-python
Cory Klein
  • 51,188
  • 43
  • 183
  • 243
Alexandr S.
  • 1,607
  • 1
  • 14
  • 16
  • 1
    Worked great. If you're getting @Johnny's python egg error, do these steps exactly but start with brew install mysql. Odds are you installed mysql using package installer on OSX. I think what this does is install mysql libs locally as you, then pip has the right libraries to compile against mysql 8. Thank you! – Paul Kenjora Nov 04 '18 at 00:19
28

please try:

brew install mysql-connector-c 
pip install MySQL-python
DàChún
  • 4,751
  • 1
  • 36
  • 39
  • 15
    Didn't work for me, I had to `brew unlink mysql` before installing mysql-connector-c, but then `pip install MySQL-python` gave "Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/sf/gzfw30z10xd6lk3zb7p9q73h0000gn/T/pip-install-VOZP1p/MySQL-python/" – Jonny Jul 11 '18 at 05:58
  • @Jonny same error, but it's worked with previous solution: https://stackoverflow.com/a/51483898/5004288 I guess sed command fixing it. – Oleg Mykolaichenko Oct 04 '18 at 12:04
13

I used brew to install everything on Mac OS 10.14.2.

The other answers weren't working for me, when inside of a virtual environment (virtualenv) on Mojave. I followed Jofsey's instructions but got errors:

#        define SIZEOF_LONG             4
                    ^
    In file included from _mysql.c:44:
    /usr/local/include/my_config.h:179:9: warning: 'SIZEOF_TIME_T' macro redefined [-Wmacro-redefined]
    #define SIZEOF_TIME_T    8
            ^
    /System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/pymacconfig.h:57:17: note: previous definition is here
    #        define SIZEOF_TIME_T           4
                    ^
    2 warnings generated.
    _mysql.c:287:14: warning: implicit conversion loses integer precision: 'Py_ssize_t' (aka 'long') to 'int' [-Wshorten-64-to-32]
                    cmd_argc = PySequence_Size(cmd_args);
                             ~ ^~~~~~~~~~~~~~~~~~~~~~~~~
    _mysql.c:317:12: warning: implicit conversion loses integer precision: 'Py_ssize_t' (aka 'long') to 'int' [-Wshorten-64-to-32]
                    groupc = PySequence_Size(groups);
                           ~ ^~~~~~~~~~~~~~~~~~~~~~~
    _mysql.c:470:14: warning: implicit conversion loses integer precision: 'Py_ssize_t' (aka 'long') to 'int' [-Wshorten-64-to-32]
                            int j, n2=PySequence_Size(fun);
                                   ~~ ^~~~~~~~~~~~~~~~~~~~
    _mysql.c:1127:9: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
                    len = mysql_real_escape_string(&(self->connection), out, in, size);
                        ~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    _mysql.c:1129:9: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
                    len = mysql_escape_string(out, in, size);
                        ~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    _mysql.c:1168:9: warning: implicit conversion loses integer precision: 'Py_ssize_t' (aka 'long') to 'int' [-Wshorten-64-to-32]
            size = PyString_GET_SIZE(s);
                 ~ ^~~~~~~~~~~~~~~~~~~~
    /System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/stringobject.h:92:32: note: expanded from macro 'PyString_GET_SIZE'
    #define PyString_GET_SIZE(op)  Py_SIZE(op)
                                   ^~~~~~~~~~~
    /System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/object.h:116:56: note: expanded from macro 'Py_SIZE'
    #define Py_SIZE(ob)             (((PyVarObject*)(ob))->ob_size)
                                     ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
    _mysql.c:1178:9: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
                    len = mysql_real_escape_string(&(self->connection), out+1, in, size);
                        ~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    _mysql.c:1180:9: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
                    len = mysql_escape_string(out+1, in, size);
                        ~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    _mysql.c:1274:11: warning: implicit conversion loses integer precision: 'Py_ssize_t' (aka 'long') to 'int' [-Wshorten-64-to-32]
            if ((n = PyObject_Length(o)) == -1) goto error;
                   ~ ^~~~~~~~~~~~~~~~~~
    /System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/abstract.h:434:25: note: expanded from macro 'PyObject_Length'
    #define PyObject_Length PyObject_Size
                            ^
    _mysql.c:1466:10: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
                            len = strlen(buf);
                                ~ ^~~~~~~~~~~
    _mysql.c:1468:10: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
                            len = strlen(buf);
                                ~ ^~~~~~~~~~~
    _mysql.c:1504:11: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
                                    len = strlen(buf);
                                        ~ ^~~~~~~~~~~
    _mysql.c:1506:11: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
                                    len = strlen(buf);
                                        ~ ^~~~~~~~~~~
    13 warnings generated.
    cc -bundle -undefined dynamic_lookup -Wl,-F. build/temp.macosx-10.14-intel-2.7/_mysql.o -L/usr/local/Cellar/mysql-connector-c/6.1.11/lib -lmysqlclient -lssl -lcrypto -o build/lib.macosx-10.14-intel-2.7/_mysql.so
    ld: library not found for -lssl
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    error: command 'cc' failed with exit status 1

So, after I ran those commands, I ran this as the final command and it worked:

sudo pip install MySQL-Python --global-option=build_ext --global-option="-I/usr/local/opt/openssl/include" --global-option="-L/usr/local/opt/openssl/lib"

Edit:

Another solution I found on GitHub, assuming you've already run brew install openssl (which avoids the need for CLI parameters and will prevent future SSL library errors):

export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/

You can put the above in your ~/.bash_profile file and then run source ~/.bash_profile or if it's a virtual environment, you can find a way to run the export in the virtual environment shell and check the value in the virtual environment CLI with echo $LIBRARY_PATH. Once the $LIBRARY_PATH is set, you won't keep seeing these SSL library errors.

Alex W
  • 37,233
  • 13
  • 109
  • 109
10

My problem was that I was using MAMP's version of mysql and it didn't contain the dev headers. I downloaded the dmg of the official release and had it running in no time.

Jake
  • 4,134
  • 2
  • 16
  • 20
  • 3
    It feels kind of...lame to have to carry around two MySQLs. I wish there was a better work around – BenDundee Apr 11 '13 at 19:41
  • 2
    Just change to postgresql. I'm so glad I did. – Jake Apr 12 '13 at 02:24
  • 2
    Well, maybe the MAMP people could offer the dev headers as an optional download to make things like this easier. Maybe a better alternative would be to just install PHP and Apache alongside MySQL and uninstall MAMP. But in general I agree: PostgreSQL is pretty sweet. – BenDundee Apr 12 '13 at 13:55
  • MAMP is great, but it can cause SO many problems. I'm so glad you had the exact same problem I did though! :D – Chris Thompson Nov 10 '15 at 00:37
10

I checked the include folders for both mysql and mysql-connector-c installed by homebrew and was surprised to see there is NO file named my_config.h. Thus, I think existing high-voted answers can no longer solve the problem.

Here is my solution for mysql 8.0.19. I copied the mysql.h file under the include/ folder of my installed mysql-connector-c and saved it as my_config.h under the same folder. Then I followed the high-voted solution as below and successfully installed mysql-python 1.2.5.

brew install mysql
brew unlink mysql
brew install mysql-connector-c   # modify the include/ folder after installing the package
sed -i -e 's/libs="$libs -l "/libs="$libs -lmysqlclient -lssl -lcrypto"/g' /usr/local/Cellar/mysql/8.0.19/bin/mysql_config
pip install MySQL-python
brew unlink mysql-connector-c
brew link --overwrite mysql

At present, I only know it can successfully install the package, but I am not sure whether it is a safe solution.

Actually, mysql-python is a quite old package (the latest release is 6 years ago), I recommend other packages for interacting with mysql in python (e.g. mysql-connector-python).

沈益菌
  • 169
  • 1
  • 3
  • 10
  • 1
    This is the correct answer in late 2020. Don't bother with MySQL-Python. Just use mysqlclient or mysql-connector-python instead. In fact, the top, pinned issue on the MySQL-Python GitHub is "this project is dead": https://github.com/farcepest/MySQLdb1/issues/151. – skyler Oct 23 '20 at 15:38
  • 1
    This is not working for me on Catalina. This answer worked, which is basically installing an older mysql (5.7): https://stackoverflow.com/a/60281682/193210. – Banjer Nov 11 '20 at 16:18
  • For me it was the other answer https://stackoverflow.com/a/60281682/161972 that worked – huyz Nov 17 '20 at 13:10
  • Definitely THE correct answer. Worked for me but I had to force linking of mysql-connector-c before running pip: brew link --force mysql-connector-c – Chris Koston Dec 09 '20 at 03:26
  • AFAICT `mysql-connector-c` isn't actually useful in the slightest, fwiw. Just `ln -s /usr/local/Cellar/mysql/8.0.23_1/include/mysql/mysql.h /usr/local/Cellar/mysql/8.0.23_1/include/mysql/my_config.h`. Homebrew is such garbage about this kind of stuff. – cbmanica Jun 01 '21 at 19:41
6

On macos I followed the installation instructions at gethue. Then I downloaded my_config.h from Oracle, placed the file in /usr/local/include, and the installation was able to continue. But stopped at an unrelated missing file.

Link at Oracle updated for a newer version of MySQL 8.0.26.

kometen
  • 6,536
  • 6
  • 41
  • 51
  • 2
    Placing the header file in `/usr/local/include` worked for me – Tobias Dec 28 '20 at 19:05
  • 1
    Spent hours trying all the various solutions, and this simple solution finally worked for me. MacOS 11.4, Python 2.7.16, mysql 8.0.25, MySQL-python-1.2.5. – Alex F. May 26 '21 at 18:45
  • 1
    The link is now broken, but thank god it has been [archived on Wayback Machine](https://web.archive.org/web/20200929222635/https://dev.mysql.com/doc/dev/mysql-server/8.0.11/my__config_8h_source.html) – user5305519 Sep 02 '21 at 13:53
  • Thank you for the heads up. I updated the answer with ver. 8.0.26. – kometen Sep 02 '21 at 14:05
5

This worked for me on macOS Catalina 10.15.7 and python 2.7

brew install mysql@5.7
brew link --force mysql@5.7
PATH="/usr/local/opt/mysql@5.7/bin:$PATH" LDFLAGS="-L/usr/local/Cellar/mysql@5.7/5.7.29/lib -L/usr/local/Cellar/openssl@1.1/1.1.1d/lib" CPPFLAGS="-I/usr/local/Cellar/mysql@5.7/5.7.29/include -I/usr/local/Cellar/openssl@1.1/1.1.1d/include" pip install mysql-python
akarca
  • 1,456
  • 15
  • 12
  • This worked for me. Seems mysql-python isn't compatible with mysql 8, or at least not on python 2.7. – s29 Jan 17 '22 at 09:46
  • this worked for me , Python 2.7.16, macOS BigSur 11.4 (20F71) ``` brew install mysql@5.7 export LDFLAGS="-L/usr/local/opt/mysql@5.7/lib" export CPPFLAGS="-I/usr/local/opt/mysql@5.7/include" echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.zshrc brew link --force mysql@5.7 PATH="/usr/local/opt/mysql@5.7/bin:$PATH" LDFLAGS="-L/usr/local/Cellar/mysql@5.7/5.7.39/lib -L/usr/local/Cellar/openssl@1.1/1.1.1q/lib" CPPFLAGS="-I/usr/local/Cellar/mysql@5.7/5.7.39/include -I/usr/local/Cellar/openssl@1.1/1.1.1q/include" pip install mysql-python ``` – rhinoceros.xn May 29 '23 at 04:24
2

I tried all the way from answers,that does't work for me.

  1. Just download mysql from https://dev.mysql.com/downloads/mysql/ and install in your mac. do not use brew install mysql

  2. install MySQL-python

    pip install MySQL-python  
    

every thing done

dovefi Z
  • 21
  • 1
2

What worked for me in Nov 2020 for Catalina 10.15.7 and Python 2.7.16 was a combination of two answers:

Step 1: https://stackoverflow.com/a/61800247/14686220

However, instead of simply copying mysql.h file as my_config.h in the include/ directory (which can be dangerous), I copied the my_config.h file from the official MySQL docs

Step 2: https://stackoverflow.com/a/54079052/14686220

nacharya
  • 21
  • 4
2

First of all clear any of the existing lib using:

brew uninstall mysql
brew uninstall mysql-client
brew uninstall mysql-connector-c++

Secondly, install sql and ensure mysql is symlinked using:

brew install mysql 
brew link mysql

Next, move to Downloads and download config file using:

cd ~/Downloads
curl https://raw.githubusercontent.com/jacob5412/MySQL-Medium/main/my_config.h -o my_config.h

Check your installed mysql version using:

mysql --version

This should return something like

mysql Ver 8.0.29 for osx12.4 on x86_64 (Homebrew)

Now, go to the directory using your version of mysql with the cmd:

cd /usr/local/Cellar/mysql/8.0.29_1/include/mysql

and copy the config file Downloaded

cp ~/Downloads/my_config.h /usr/local/Cellar/mysql/8.0.29_1/include/mysql/

We are almost done now, finally install mysql:

pip install MySQL-python

And congratulations, we fixed the issue.

Thanks to Jacob for the solution. Reference

0

As mentioned before, you need the dev headers for MySQL, which don't come with MAMP by default. Rather than using two instances of MySQL, it's possible to add the headers to the MAMP version. There are good instructions for doing so here: http://dreamconception.com/tech/how-to-install-mysqldb-mysql-python-on-mamp/

Make sure not to just copy-paste all the commands, they're a bit out of date so you'll need to change the version numbers in some of them.

I'm using OS X 10.9 and python 2.7, and everything works.

sdlwdr
  • 1
  • 1
  • 3
0

On macOS 10.15 so long as your project is compatible with PyMySQL 1.3.11 or newer, the following appears to resolve this problem:

brew install mariadb-connector-c
pip install MySQL-python

Ref: https://github.com/PyMySQL/mysqlclient-python/blob/master/HISTORY.rst

ic_
  • 31
  • 2
0

Using conda instead of pip solved this for me on macOS Catalina 10.15.5

conda install mysql-python
dd.
  • 826
  • 9
  • 13