5

I'm trying to run on Mac Os high Sierra

pip install MySQL-python

But I'm getting

error: command 'cc' failed with exit status 1

I already tried the solution on this topic, but it didn't work.

This is the full error message

The directory '/Users/filipeferminiano/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/filipeferminiano/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting MySQL-python
  Downloading https://files.pythonhosted.org/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip (108kB)
    100% |████████████████████████████████| 112kB 289kB/s 
Installing collected packages: MySQL-python
  Running setup.py install for MySQL-python ... error
    Complete output from command /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python -u -c "import setuptools, tokenize;__file__='/private/tmp/pip-install-8I1X5u/MySQL-python/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /private/tmp/pip-record-z5HohX/install-record.txt --single-version-externally-managed --compile:
    running install
    running build
    running build_py
    creating build
    creating build/lib.macosx-10.13-intel-2.7
    copying _mysql_exceptions.py -> build/lib.macosx-10.13-intel-2.7
    creating build/lib.macosx-10.13-intel-2.7/MySQLdb
    copying MySQLdb/__init__.py -> build/lib.macosx-10.13-intel-2.7/MySQLdb
    copying MySQLdb/converters.py -> build/lib.macosx-10.13-intel-2.7/MySQLdb
    copying MySQLdb/connections.py -> build/lib.macosx-10.13-intel-2.7/MySQLdb
    copying MySQLdb/cursors.py -> build/lib.macosx-10.13-intel-2.7/MySQLdb
    copying MySQLdb/release.py -> build/lib.macosx-10.13-intel-2.7/MySQLdb
    copying MySQLdb/times.py -> build/lib.macosx-10.13-intel-2.7/MySQLdb
    creating build/lib.macosx-10.13-intel-2.7/MySQLdb/constants
    copying MySQLdb/constants/__init__.py -> build/lib.macosx-10.13-intel-2.7/MySQLdb/constants
    copying MySQLdb/constants/CR.py -> build/lib.macosx-10.13-intel-2.7/MySQLdb/constants
    copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.macosx-10.13-intel-2.7/MySQLdb/constants
    copying MySQLdb/constants/ER.py -> build/lib.macosx-10.13-intel-2.7/MySQLdb/constants
    copying MySQLdb/constants/FLAG.py -> build/lib.macosx-10.13-intel-2.7/MySQLdb/constants
    copying MySQLdb/constants/REFRESH.py -> build/lib.macosx-10.13-intel-2.7/MySQLdb/constants
    copying MySQLdb/constants/CLIENT.py -> build/lib.macosx-10.13-intel-2.7/MySQLdb/constants
    running build_ext
    building '_mysql' extension
    creating build/temp.macosx-10.13-intel-2.7
    cc -fno-strict-aliasing -fno-common -dynamic -arch x86_64 -arch i386 -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch x86_64 -pipe -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 -I/usr/local/Cellar/mysql/8.0.11/include/mysql -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _mysql.c -o build/temp.macosx-10.13-intel-2.7/_mysql.o
    _mysql.c:44:10: fatal error: 'my_config.h' file not found
    #include "my_config.h"
             ^~~~~~~~~~~~~
    1 error generated.
    error: command 'cc' failed with exit status 1

    ----------------------------------------
Command "/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python -u -c "import setuptools, tokenize;__file__='/private/tmp/pip-install-8I1X5u/MySQL-python/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /private/tmp/pip-record-z5HohX/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/tmp/pip-install-8I1X5u/MySQL-python/

This is the python

which python
/usr/bin/python

and this is the pip I'm using

which pip
/usr/local/bin/pip
Filipe Ferminiano
  • 8,373
  • 25
  • 104
  • 174
  • Please give us the full error. All we can tell from this is that your compiler failed in some way while doing something. – abarnert Jul 14 '18 at 21:07
  • 1
    Have you installed Xcode and Command Line Tools? Install Xcode from AppStore then do `xcode-select --install` in Terminal. – Mark Setchell Jul 14 '18 at 21:08
  • Also, please tell us about your setup. Is your `pip` installing for Apple's pre-installed Python 2.7, or another 2.7 (if so, python.org? Anaconda? Homebrew? …?), or even a non-2.7 version? (And how are you sure of that?) Do you _have_ multiple Pythons? Did you install Xcode, the Xcode Command Line Tools, both, or neither? Have you installed any other compilers (e.g., `brew install gcc`)? What version of MySQL do you have, and how did you install that? – abarnert Jul 14 '18 at 21:09
  • @abarnert I updated the question with full message. I also updated with which python and pip I'm using. I already have XCode and XCode Command Line. I didn't installed the gcc. – Filipe Ferminiano Jul 15 '18 at 00:58
  • Install the MySQL development files. – Ignacio Vazquez-Abrams Jul 15 '18 at 01:25
  • I am facing the same problem. I had used ubuntu before and now I am unable to do it on mac. Please post the solution if you have solved it – The Bat Jul 15 '18 at 10:40
  • @IgnacioVazquez-Abrams How do we do that? – The Bat Jul 15 '18 at 10:40
  • As a side note: Are you sure you want to use Apple's pre-installed Python 2.7.10, instead of a more-normally-configured 2.7.15, or a 3.x version? – abarnert Jul 15 '18 at 19:57
  • One more side note: those first two lines in your `pip` output are an unrelated warning that comes from arbitrarily mixing `pip` and `sudo pip` until you manage to confuse things. You probably want to fix it—it won't _break_ anything, but it means you're forcing `pip` to repeat a lot of unnecessary work. (If you're not sure how to do that, I'm sure there's a question here that explains it—or, if not, post a new question.) – abarnert Jul 15 '18 at 20:06

3 Answers3

9

First, are you sure you want to use MySQL-python? This is a mostly-dead project that's been semi-maintained for legacy support for the past half decade, and doesn't even work with current versions of MySQL. It wants 5.0 or maybe 5.1; the current version is 8.0, and even the current legacy version is 5.7.


Other options include:

  • mysql-connector, aka MySQLConnector/Python: This is the officially supported library, from MySQL/Oracle. It can be a bit slow, which doesn't matter for most projects, but it can sometimes. It also has a different API (although if you stick to DB-API 2 methods, the only difference is the connect call; beyond that it should just be low-level stuff that's different).
  • mysqlclient: This is the package that Django and some other frameworks use. It's an updated fork of MySQL-Python (with a few things from its abandoned successor, moist). It can even be configured to install itself as MySQLdb, the same name used by MySQL-python.
  • PyMySQL: A third-party package built to be as compatible as possible with the old MySQL-python but simpler and easier to maintain, and install.
  • cmysql, a fork of PyMySQL that's only slightly harder to install, but should be faster than it, or than mysql-connector.

Some linux distros—including, IIRC, recent versions of Ubuntu—provide a package named python-MySQLdb or similar that is not actually MySQL-python, but instead mysqlclient built with the install-as-MySQLdb option. So, if the only reason that you're trying to use MySQL-python is that it's what you were using on some Ubuntu box, it's probably not what you were using, and therefore not what you want.

Some of these alternative also require MySQL (specifically, MySQLConnector/C, aka libmysqlclient, and its development libraries), some don't—but they're all compatible with current versions. (Although there are some notes on 8.0 crypto changes in the docs for PyMySQL and cmysql, which you might want to read if you use 8.0.)


Anyway, if you really want MySQL-python, then you will need to install MySQL in a 5.x version. The INSTALL points you to MySQL downloads.

If you want 5.0 or 5.1, you will have to dig through the Downloads folder at one of the mirrors to find a source package and follow the instructions to build and install it, since there are no binary installers that work on current Macs.

If you want later 5.x versions—which, remember, are not supported by MySQL-python, but they might work—Oracle is still providing Mac binary installers for those. If you can't find them in the main downloads section, the mirrors' Downloads folder will have them. As of right now, 5.6 and 5.7 have binary installer packages that end with -macos10.13-x86_64.dmg.

However, you might be happier installing it with Homebrew. Follow the instructions on that page to install brew, then to use it to:

brew install mysql@5.5

MySQL-python also requires OpenSSL libraries, and Apple deliberately hides the ones used by the OS to prevent people from accidentally building code against an old version and not getting security updates. The newer libraries should all know how to handle this, but MySQL-python does not. The easiest way to install a usable OpenSSL is with Homebrew again:

brew install openssl

But don't just run that command and close the window. Read the output, because you have to do all the stuff it says if you want MySQL-python to build.

abarnert
  • 354,177
  • 51
  • 601
  • 671
1

My issue was that I was using MySql Version 8.0.12. As soon as I downgraded to MySQL 5.7, it was installed perfectly. I used the following to delete the version 8 by following: https://gist.github.com/vitorbritto/0555879fe4414d18569d

The gist of the above was:

  1. Check for MySQL processes with: ps -ax | grep mysql
  2. Stop and kill any MySQL processes
  3. brew remove mysql
  4. brew cleanup
  5. launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
  6. sudo rm /usr/local/mysql
  7. sudo rm -rf /usr/local/var/mysql
  8. sudo rm -rf /usr/local/mysql*
  9. sudo rm ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
  10. sudo rm -rf /Library/StartupItems/MySQLCOM
  11. sudo rm -rf /Library/PreferencePanes/My*
  12. rm -rf ~/Library/PreferencePanes/My*
  13. sudo rm -rf /Library/Receipts/mysql*
  14. sudo rm -rf /Library/Receipts/MySQL*
  15. sudo rm -rf /private/var/db/receipts/mysql
  16. Restart your computer just to ensure any MySQL processes are killed
  17. install mysql using brew: brew install mysql@5.7
  18. Finally, pip install mysql-python
Ashwin_Mathew
  • 50
  • 1
  • 8
0

abamert's answer did not work for me. Though this did, you might try skipping the first step,

brew install mysql-connector-c
brew install mysql
brew link --overwrite mysql
pip install MySQL-python
Optimus Prime
  • 6,817
  • 5
  • 32
  • 60