26

I have installed tools via xcode-select --install but still show the following error while doing pip install MySQL-python

Building wheels for collected packages: MySQL-python
  Running setup.py bdist_wheel for MySQL-python ... error
  Complete output from command /Users/vaibhavmule/Envs/switchidea/bin/python2.7 -u -c "import setuptools, tokenize;__file__='/private/var/folders/p6/0v0bflxn3t399_qdpnm2z7hc0000gn/T/pip-install-SD9Cgh/MySQL-python/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /private/var/folders/p6/0v0bflxn3t399_qdpnm2z7hc0000gn/T/pip-wheel-g6C50k --python-tag cp27:
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.macosx-10.13-x86_64-2.7
  copying _mysql_exceptions.py -> build/lib.macosx-10.13-x86_64-2.7
  creating build/lib.macosx-10.13-x86_64-2.7/MySQLdb
  copying MySQLdb/__init__.py -> build/lib.macosx-10.13-x86_64-2.7/MySQLdb
  copying MySQLdb/converters.py -> build/lib.macosx-10.13-x86_64-2.7/MySQLdb
  copying MySQLdb/connections.py -> build/lib.macosx-10.13-x86_64-2.7/MySQLdb
  copying MySQLdb/cursors.py -> build/lib.macosx-10.13-x86_64-2.7/MySQLdb
  copying MySQLdb/release.py -> build/lib.macosx-10.13-x86_64-2.7/MySQLdb
  copying MySQLdb/times.py -> build/lib.macosx-10.13-x86_64-2.7/MySQLdb
  creating build/lib.macosx-10.13-x86_64-2.7/MySQLdb/constants
  copying MySQLdb/constants/__init__.py -> build/lib.macosx-10.13-x86_64-2.7/MySQLdb/constants
  copying MySQLdb/constants/CR.py -> build/lib.macosx-10.13-x86_64-2.7/MySQLdb/constants
  copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.macosx-10.13-x86_64-2.7/MySQLdb/constants
  copying MySQLdb/constants/ER.py -> build/lib.macosx-10.13-x86_64-2.7/MySQLdb/constants
  copying MySQLdb/constants/FLAG.py -> build/lib.macosx-10.13-x86_64-2.7/MySQLdb/constants
  copying MySQLdb/constants/REFRESH.py -> build/lib.macosx-10.13-x86_64-2.7/MySQLdb/constants
  copying MySQLdb/constants/CLIENT.py -> build/lib.macosx-10.13-x86_64-2.7/MySQLdb/constants
  running build_ext
  building '_mysql' extension
  creating build/temp.macosx-10.13-x86_64-2.7
  clang -fno-strict-aliasing -fno-common -dynamic -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -Dversion_info=(1,2,3,'final',0) -D__version__=1.2.3 -I/usr/local/Cellar/mysql/8.0.11/include/mysql -I/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _mysql.c -o build/temp.macosx-10.13-x86_64-2.7/_mysql.o
  _mysql.c:36:10: fatal error: 'my_config.h' file not found
  #include "my_config.h"
           ^~~~~~~~~~~~~
  1 error generated.
  error: command 'clang' failed with exit status 1

  ----------------------------------------
  Failed building wheel for MySQL-python
  Running setup.py clean for MySQL-python
Failed to build MySQL-python
django-model-utils 3.0.0 has requirement Django>=1.8, but you'll have django 1.6.7 which is incompatible.
faker 0.8.16 has requirement six>=1.10, but you'll have six 1.6.1 which is incompatible.
django-phonenumber-field 2.0.0 has requirement Django>=1.11, but you'll have django 1.6.7 which is incompatible.
Installing collected packages: MySQL-python, opbeat, olefile, Pillow, pyzmq, contextlib2, raven, simplejson, South, sqlparse, wheel, django-sendfile, httplib2, pytz, twilio, phonenumberslite, babel, django-phonenumber-field, django-twilio, text-unidecode, python-dateutil, Faker, factory-boy, waitress, beautifulsoup4, WebOb, webtest, django-webtest, coverage
  Running setup.py install for MySQL-python ... error
    Complete output from command /Users/vaibhavmule/Envs/switchidea/bin/python2.7 -u -c "import setuptools, tokenize;__file__='/private/var/folders/p6/0v0bflxn3t399_qdpnm2z7hc0000gn/T/pip-install-SD9Cgh/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/var/folders/p6/0v0bflxn3t399_qdpnm2z7hc0000gn/T/pip-record-ihy3we/install-record.txt --single-version-externally-managed --compile --install-headers /Users/vaibhavmule/Envs/switchidea/bin/../include/site/python2.7/MySQL-python:
    running install
    running build
    running build_py
    creating build
    creating build/lib.macosx-10.13-x86_64-2.7
    copying _mysql_exceptions.py -> build/lib.macosx-10.13-x86_64-2.7
    creating build/lib.macosx-10.13-x86_64-2.7/MySQLdb
    copying MySQLdb/__init__.py -> build/lib.macosx-10.13-x86_64-2.7/MySQLdb
    copying MySQLdb/converters.py -> build/lib.macosx-10.13-x86_64-2.7/MySQLdb
    copying MySQLdb/connections.py -> build/lib.macosx-10.13-x86_64-2.7/MySQLdb
    copying MySQLdb/cursors.py -> build/lib.macosx-10.13-x86_64-2.7/MySQLdb
    copying MySQLdb/release.py -> build/lib.macosx-10.13-x86_64-2.7/MySQLdb
    copying MySQLdb/times.py -> build/lib.macosx-10.13-x86_64-2.7/MySQLdb
    creating build/lib.macosx-10.13-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/__init__.py -> build/lib.macosx-10.13-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/CR.py -> build/lib.macosx-10.13-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.macosx-10.13-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/ER.py -> build/lib.macosx-10.13-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/FLAG.py -> build/lib.macosx-10.13-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/REFRESH.py -> build/lib.macosx-10.13-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/CLIENT.py -> build/lib.macosx-10.13-x86_64-2.7/MySQLdb/constants
    running build_ext
    building '_mysql' extension
    creating build/temp.macosx-10.13-x86_64-2.7
    clang -fno-strict-aliasing -fno-common -dynamic -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -Dversion_info=(1,2,3,'final',0) -D__version__=1.2.3 -I/usr/local/Cellar/mysql/8.0.11/include/mysql -I/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _mysql.c -o build/temp.macosx-10.13-x86_64-2.7/_mysql.o
    _mysql.c:36:10: fatal error: 'my_config.h' file not found
    #include "my_config.h"
             ^~~~~~~~~~~~~
    1 error generated.
    error: command 'clang' failed with exit status 1

    ----------------------------------------
Command "/Users/vaibhavmule/Envs/switchidea/bin/python2.7 -u -c "import 
setuptools, tokenize;__file__='/private/var/folders/p6/0v0bflxn3t399_qdpnm2z7hc0000gn/T/pip-install-SD9Cgh/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/var/folders/p6/0v0bflxn3t399_qdpnm2z7hc0000gn/T/pip-record-ihy3we/install-record.txt --single-version-externally-managed --compile --install-headers /Users/vaibhavmule/Envs/switchidea/bin/../include/site/python2.7/MySQL-python" failed with error code 1 in /private/var/folders/p6/0v0bflxn3t399_qdpnm2z7hc0000gn/T/pip-install-SD9Cgh/MySQL-python/
Shadow
  • 33,525
  • 10
  • 51
  • 64
Vaibhav Mule
  • 5,016
  • 4
  • 35
  • 52

8 Answers8

68

I just resolved this exact issue when running Ansible's mysql_user module. The answer here helped tip me off to a solution. I also needed MySQL-python from pip, which also broke in this process, so I've added the extra steps.

Steps to resolve:

  1. brew unlink mysql # only if installed, causes the next step to fail
  2. brew install mysql-connector-c
  3. locate mysql_config file with which (mysql_config)
  4. edit the mysql_config file, under # Create options change this:

    libs="$libs -l "

    to this:

    libs="$libs -lmysqlclient -lssl -lcrypto"

    if using vim, :wq! to save the read-only file

  5. Now the install should run successfully

    pip install mysqlclient

  6. Adding this separately, as it's similar but not directly related to the initial question

    pip install MySQL-python

  7. Fix mysql brew formula, if it was unlinked in the first step.

    brew unlink mysql-connector-c

    brew link mysql

sbaxter
  • 806
  • 7
  • 5
  • Along with I need to link `mysql-connector-c` like `brew link mysql-connector-c` – Vaibhav Mule Jun 23 '18 at 12:58
  • 29
    It is still failing at step 5 for me. So I install openssl by `brew install openssl` and then I set ssl library path explicitly `LDFLAGS=-L/usr/local/opt/openssl/lib pip install mysqlclient` and now it's working. – Vishal Nagda Nov 28 '18 at 10:20
  • 1
    import MySQLdb throws an error "this is MySQLdb version (1, 2, 5, 'final', 1), but _mysql is version (1, 4, 2, 'post', 1)" anyways – Manoj Jun 18 '19 at 13:28
  • 1
    Just a note for a different MacOS version (namely Catalina 10.15.0 and mysql 8.0.18), there is no need to perform any additional steps. Just `brew install mysql` and `mysql_config` should be visible by default, as it is located at `/usr/local/bin` (that's assuming `/usr/local/bin` is part of your PATH environment variable) – Sian Lerk Lau Nov 14 '19 at 09:59
  • 1
    For me, step 4 (updating options in mysql_config) was not necessary, as those options were already there. I had to add `mysql-client` to my path after step 2 before the `pip install mysqlclient` would work however: `echo 'export PATH="/usr/local/opt/mysql-client/bin:$PATH"' >> ~/.zshrc` – Charlie A Feb 10 '21 at 20:44
  • UPDATE: If you have Linux/Mac: Please open the /usr/local/bin/mysql_config. – Srinivas Jayaram Feb 23 '21 at 19:30
20

I fixed it by passing the OpenSSL path:

env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip install mysqlclient

You can see the full article of the solution done by MrWeeble

7

I solved this issue by setting xcode-select to use non-XCode command line tools, as shown in the below answer. I had a related clang error which led me to this answer.

sudo xcode-select --switch /Library/Developer/CommandLineTools

https://stackoverflow.com/a/30902106/11614142

NPE_Exception
  • 111
  • 2
  • 5
  • Nice catch! Was causing all kinds of other issues for me. Prevents you having to use the flag in the comments above. – PJATX Dec 27 '19 at 23:30
  • THANK YOU! I was running into this issue while trying to pip install dbt. This worked for me! – mdrishan Jan 06 '21 at 15:54
3

I know maybe the former answers can solve the problem already. But I'm still sharing below solution. If you don't want to brew install any extra things, you can try this solution.

I think the issue happens in many situations, mainly related to the installation of the pip packages about MySQL. In my case, I bumped into this issue when trying to install mysqlclient for Django.

This solution should work when you've installed MySQL with dmg downloaded from their official site.

In that case, you may find mysql_config in /usr/local/mysql/bin

If so, add export PATH="/usr/local/mysql/bin:${PATH}" in ~/.bash_profile, this would add the bin folder of mysql into the PATH, or you can say environment variable. Please note that mysql maybe in detail with your mysql version. In my case, the path is /usr/local/mysql-8.0.12-macos10.13-x86_64/bin.

Save and close the file, then source ~/.bash_profile to enable the change.

Try to install the pip package about MySQL again.

If still not working, a restart is preferred.

1

None of these answers are really needed in that length. As Vishal noted it is as easy as

brew install openssl 
LDFLAGS=-L/usr/local/opt/openssl/lib pip install mysqlclient

Then go back to install your pip install -r requirements.txt

Houman
  • 64,245
  • 87
  • 278
  • 460
1

So first what is clang? it is a "compiler frontend" for C, C++. What is a compiler frontend you may ask? See this answer for more details. You're trying to install a library (mysqlclient or apache-airflow[mysql] or any other) that requires some C++ dependency to be found but is not, hence the message that looks like:

    #include "my_config.h"
             ^~~~~~~~~~~~~

The clang error messages are pretty verbose so you might miss the important information. What's important are those 3 lines:

    ld: library not found for -lzstd
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    error: command 'clang' failed with exit status 1

Find that first line before "command 'clang' failed", this will tell you what library was not found.

If the library was not found, it could be because it's not installed (hence all the brew install ... answers here) or because it's not found in the path (hence all the export LDFLAGS=... and export LIBRARY_PATH=... answers).

In my case, 2 librairies were not found but both were installed, so I solved it with:

export LDFLAGS="-L/usr/local/Cellar/openssl@1.1/1.1.1l/lib -L/usr/local/Cellar/zstd/1.5.0/lib"
pip install 'apache-airflow[mysql]'

This is not a direct answer to OP, but hopefully helpful in some way.

louis_guitton
  • 5,105
  • 1
  • 31
  • 33
1

I tried this,

export ARCHFLAGS="-arch x86_64"

Then re-execute the installation command. It's resolved for me.

Rahul K P
  • 15,740
  • 4
  • 35
  • 52
0

Following did the trick for me, Using mac.

brew install leveldb gmp pkg-config

After this just run

pip3 install MySQL-python
Vimal
  • 11
  • 1
  • 2