Mac 安装MySQL-python 的坑

2,022 阅读1分钟

安装环境情况

安装命令

pip install MySQL-python

这样通常是不行的

安装前的准备

0. brew install mysql
1. brew unlink mysql # 如果你有安装brew install mysql 的话
2. brew install mysql-connector-c # 安装mysql依赖的一些文件
3. which mysql_config  # 查看本地mysql_config 文件在哪
4. vim mysql_config文件,找到
    libs="$libs -l "
    替换为
    libs="$libs -lmysqlclient -lssl -lcrypto"
    # 新版本mysql可能已经为替换后的则无需修改
5. pip install mysqlclient
6. pip install MySQL-python
7. brew unlink mysql-connector-c
8. brew link mysql

常见错误

  _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

错误原因

1. 未安装mysql-connector-c 缺少对应的库

2. 安装了新版的mysql 默认缺少了my_config文件

这是mysql 8.0.19 默认库文件,确实没有my_config.h,这时候我们需要copy一份

将mysql.h copy 一份重命名为 my_config.h

这时候应该就没问题了。~

也是参考于stackoverflow,附上原链接 stackoverflow

感谢你的时间看完~