安装 MySQL-python 导致命令 'clang' 在 Mac OS 10.13.15 上失败,退出状态为 1

Installing MySQL-python causes command 'clang' failed with exit status 1 on Mac OS 10.13.15

我已经通过 xcode-select --install 安装了工具,但在执行 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/

当 运行ning Ansible 的 mysql_user module. The answer 帮助我找到解决方案时,我刚刚解决了这个确切的问题。我还需要 pip 中的 MySQL-python,它在此过程中也中断了,所以我添加了额外的步骤。

解决步骤:

  1. brew unlink mysql # only if installed, causes the next step to fail
  2. brew install mysql-connector-c
  3. which (mysql_config)
  4. 找到 mysql_config 文件
  5. 编辑 mysql_config 文件,在 # Create options 下更改为:

    libs="$libs -l "

    对此:

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

    如果使用vim,:wq!保存只读文件

  6. 现在安装应该运行成功

    pip install mysqlclient

  7. 单独添加,因为它与最初的问题相似但没有直接关系

    pip install MySQL-python

  8. 修复 mysql brew 公式,如果它在第一步中未链接。

    brew unlink mysql-connector-c

    brew link mysql

我知道前面的答案可能已经可以解决问题了。但我仍在分享以下解决方案。如果你不想 brew install 任何额外的东西,你可以试试这个解决方案。

我认为这个问题在很多情况下都会发生,主要与安装有关MySQL的pip包有关。就我而言,我在尝试为 Django 安装 mysql 客户端时遇到了这个问题。

当您安装 MySQL 并使用从其官方网站下载的 dmg 时,此解决方案应该有效。

在这种情况下,您可能会在 /usr/local/mysql/bin

中找到 mysql_config

如果是这样,在~/.bash_profile中添加export PATH="/usr/local/mysql/bin:${PATH}",这会将mysql的bin文件夹添加到PATH中,或者你可以说环境变量。请注意 mysql 可能与您的 mysql 版本有关。在我的例子中,路径是 /usr/local/mysql-8.0.12-macos10.13-x86_64/bin.

保存并关闭文件,然后source ~/.bash_profile启用更改。

重新尝试安装MySQL的pip包

如果还是不行,建议重启。

我通过设置 xcode-select 来使用非 XCode 命令行工具解决了这个问题,如下面的答案所示。我有一个相关的 clang 错误导致我得到这个答案。

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

我通过传递 OpenSSL 路径修复了它:

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

None 这些答案在那个长度上是真正需要的。正如 Vishal 指出的那样简单

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

然后回去安装你的pip install -r requirements.txt

那么首先什么是clang?它是 C、C++ 的“编译器前端”。您可能会问什么是编译器前端?有关详细信息,请参阅 this answer。您正在尝试安装一个库(mysqlclientapache-airflow[mysql] 或任何其他库),该库需要找到 一些 C++ 依赖项但不是,因此消息看起来像:

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

clang 错误消息非常冗长,因此您可能会错过重要信息。重要的是那 3 行:

    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

找到“command 'clang' failed”之前的第一行,这会告诉你找不到什么库。

如果找不到该库,可能是因为它没有安装(因此所有 brew install ... 答案都在这里)或者因为它在路径中找不到(因此所有 export LDFLAGS=...export LIBRARY_PATH=... 个答案)。

在我的例子中,没有找到 2 个库,但都安装了,所以我解决了这个问题:

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]'

这不是对 OP 的直接回答,但希望在某种程度上有所帮助。

我试过了,

export ARCHFLAGS="-arch x86_64"

然后重新执行安装命令。帮我解决了。

以下对我有用,使用 mac。

brew install leveldb gmp pkg-config

之后就是运行

pip3 install MySQL-python