命令 'cc' 在 OSX High Sierra 上失败,退出状态为 1

command 'cc' failed with exit status 1 on OSX High Sierra

我正在尝试 运行 Mac Os high Sierra

pip install MySQL-python

但是我得到了

error: command 'cc' failed with exit status 1

我已经在 this topic 上尝试过该解决方案,但没有成功。

这是完整的错误信息

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/

这是python

which python
/usr/bin/python

这是我正在使用的 pip

which pip
/usr/local/bin/pip

首先,您确定要使用MySQL-python吗?这是一个半死不活的项目,在过去的五年里一直在半维护遗留支持,甚至不能与 MySQL 的当前版本一起使用。它想要 5.0 或 5.1;当前版本是8.0,甚至当前的遗留版本是5.7。


其他选项包括:

  • mysql-connector,又名 MySQLConnector/Python:这是官方支持的库,来自 MySQL/Oracle。它可能有点慢,这对大多数项目来说无关紧要,但有时会这样。它也有一个不同的 API(尽管如果你坚持使用 DB-API 2 方法,唯一的区别是连接调用;除此之外它应该只是不同的低级内容)。
  • mysqlclient:这是Django和其他一些框架使用的包。它是 MySQL-Python 的更新分支(有一些来自其废弃的继任者 moist 的内容)。它甚至可以配置为将自己安装为 MySQLdb,与 MySQL-python.
  • 使用的名称相同
  • PyMySQL:构建的第三方软件包尽可能与旧 MySQL-python 兼容,但更简单、更易于维护和安装。
  • cmysqlPyMySQL 的一个分支,安装起来稍微有点困难,但应该比它快,或者比 mysql-connector.

一些 linux 发行版——包括 IIRC,Ubuntu 的最新版本——提供了一个名为 python-MySQLdb 或类似名称的包,实际上不是 MySQL-python,而是使用 install-as-MySQLdb 选项构建的 mysqlclient。因此,如果您尝试使用 MySQL-python 的唯一原因是它是您在某些 Ubuntu 盒子上使用的,那么它可能 而不是 使用,因此不是您想要的。

其中一些替代方案还需要 MySQL(具体来说,MySQLConnector/C,又名 libmysqlclient 及其开发库),有些则不需要——但它们都与当前版本。 (虽然在 PyMySQL 和 cmysql 的文档中有一些关于 8.0 加密更改的注释,如果您使用 8.0,您可能需要阅读这些注释。)


无论如何,如果你真的想要MySQL-python,那么你需要在5.x版本中安装MySQL。 INSTALL 指向 MySQL downloads.

如果你想要 5.0 或 5.1,你将不得不在其中一个镜像中挖掘 Downloads 文件夹以找到源包并按照说明构建和安装它,因为没有二进制文件适用于当前 Macs.

的安装程序

如果您想要更高的 5.x 版本——请记住,MySQL-python 不支持这些版本,但它们可能有效——Oracle 仍在提供 Mac那些的二进制安装程序。如果您在主下载部分找不到它们,镜像的下载文件夹中会有它们。截至目前,5.6 和 5.7 具有以 -macos10.13-x86_64.dmg.

结尾的二进制安装程序包

但是,您可能更愿意使用 Homebrew 安装它。按照该页面上的说明安装 brew,然后将其用于:

brew install mysql@5.5

MySQL-python 需要 OpenSSL 库,Apple 故意隐藏了 OS 使用的那些,以防止人们不小心针对旧版本构建代码并且没有获得安全更新。较新的库应该都知道如何处理这个,但是 MySQL-python 不知道。安装可用 OpenSSL 的最简单方法是再次使用 Homebrew:

brew install openssl

但不要只是 运行 该命令并关闭 window。阅读输出,因为如果你想要构建 MySQL-python,你必须执行它所说的所有内容。

abamert 的回答对我不起作用。虽然这样做了,但您可以尝试跳过第一步,

brew install mysql-connector-c
brew install mysql
brew link --overwrite mysql
pip install MySQL-python

我的问题是我使用的是 MySql 版本 8.0.12。我一降到MySQL5.7就完美安装了。我使用以下命令删除版本 8:https://gist.github.com/vitorbritto/0555879fe4414d18569d

上面的要点是:

  1. 检查 MySQL 个进程:ps -ax | grep mysql
  2. 停止并杀死任何 MySQL 个进程
  3. 酿造删除mysql
  4. 酿造清理
  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. 重新启动您的计算机以确保所有 MySQL 进程被终止
  17. 使用 brew 安装 mysql:brew install mysql@5.7
  18. 最后,pip install mysql-python