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

Posted

技术标签:

【中文标题】在 Mac OS 10.13.15 上安装 MySQL-python 导致命令“clang”失败,退出状态为 1【英文标题】:Installing MySQL-python causes command 'clang' failed with exit status 1 on Mac OS 10.13.15 【发布时间】:2018-11-29 02:52:28 【问题描述】:

我已经通过 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/

【问题讨论】:

【参考方案1】:

我刚刚在运行 Ansible 的 mysql_user 模块时解决了这个问题。答案here 帮助我找到了解决方案。我还需要来自pipMySQL-python,这也中断了这个过程,所以我添加了额外的步骤。

解决步骤:

    brew unlink mysql # only if installed, causes the next step to fail brew install mysql-connector-c 使用which (mysql_config) 找到mysql_config 文件

    编辑mysql_config 文件,在# Create options 下更改:

    libs="$libs -l "

    到这里:

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

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

    现在安装应该会成功运行

    pip install mysqlclient

    单独添加,因为它与最初的问题相似但不直接相关

    pip install MySQL-python

    修复mysql brew 公式,如果它在第一步中被取消链接。

    brew unlink mysql-connector-c

    brew link mysql

【讨论】:

对我来说,第 5 步仍然失败。所以我通过brew install openssl 安装openssl,然后我明确设置ssl 库路径LDFLAGS=-L/usr/local/opt/openssl/lib pip install mysqlclient,现在它可以工作了。 很棒的答案。救了我 1 天。 有效。伟大的。感谢您提供详细信息。 import MySQLdb 抛出错误“这是 MySQLdb 版本 (1, 2, 5, 'final', 1),但 _mysql 是版本 (1, 4, 2, 'post', 1)”反正 只是对不同MacOS版本(即Catalina 10.15.0和mysql 8.0.18)的注释,不需要执行任何额外的步骤。默认情况下,brew install mysqlmysql_config 应该是可见的,因为它位于 /usr/local/bin(假设 /usr/local/bin 是 PATH 环境变量的一部分)【参考方案2】:

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

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

当您使用从官方网站下载的 dmg 安装 MySQL 时,此解决方案应该可以工作。

在这种情况下,您可能会在/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包。

如果还是不行,最好重启一下。

【讨论】:

同理,如果你使用brew安装mysql_client,你可能需要在路径中添加/usr/local/opt/mysql-client/bin【参考方案3】:

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

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

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

【讨论】:

不错的收获!给我带来了各种各样的其他问题。防止您必须在上面的 cmets 中使用标志。 谢谢!我在尝试 pip install dbt 时遇到了这个问题。这对我有用!【参考方案4】:

我通过传递 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

【讨论】:

【参考方案5】:

这些答案都不是真正需要的。正如 Vishal 所说,它就像

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

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

【讨论】:

【参考方案6】:

那么首先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 的直接回答,但希望在某些方面有所帮助。

【讨论】:

【参考方案7】:

我试过了,

export ARCHFLAGS="-arch x86_64"

然后重新执行安装命令。已经为我解决了。

【讨论】:

以上是关于在 Mac OS 10.13.15 上安装 MySQL-python 导致命令“clang”失败,退出状态为 1的主要内容,如果未能解决你的问题,请参考以下文章

如何在Mac OS X上安装 Ruby运行环境

如何在Mac OS X Yosemite上安装 jdk 1.6 开发环境

如何在Mac OS X上部署开发环境

Windows上安装Mac OS

sh 在Mac OS上安装打包程序

sh 在mac os上安装composer