注意:如果你用的是python3.x,直接去看第四个问题
遇到的第一个问题
解决第一个问题
遇到第二个问题
-
brew install mysql-connector-c报错
Error: Cannot install mysql-connector-c because conflicting formulae are installed.
mysql: because both install MySQL client libraries
Please `brew unlink mysql` before continuing.
解决第二个问题
- 按照报错的提示,执行
brew unlink mysql
- 没有发生什么意外,执行完毕,继续执行
brew install mysql-connector-c
- 'mysql-connector-c'安装成功
- 执行
brew link --overwrite mysql,重新连接mysql(这一步我没有做)
- 然后再执行
pip install MySQL-python,如果成功了就搞定了
- 神奇的是,我在这一步又失败了
遇到的第三个问题
-
上面的步骤走完以后,执行pip install MySQL-python,报错
Collecting mysql
Downloading https://files.pythonhosted.org/packages/06/ef/c4efbf2a51fb46aba9be03a973638d9539c9ca10a5259b2cbb1a66133b2e/mysql-0.0.1.tar.gz
Collecting MySQL-python (from mysql)
Using cached https://files.pythonhosted.org/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/private/var/folders/zn/t8xxx4m149s9jqp1810ndrz80000gn/T/pip-install-oHMKPE/MySQL-python/setup.py", line 17, in <module>
metadata, options = get_config()
File "setup_posix.py", line 53, in get_config
libraries = [ dequote(i[2:]) for i in libs if i.startswith(compiler_flag("l")) ]
File "setup_posix.py", line 8, in dequote
if s[0] in "\"'" and s[0] == s[-1]:
IndexError: string index out of range
解决第三个问题
- 修改mysql的配置文件
mysql_config,修改前记得cp一下
执行mysql_config,查看一下路径
-
打开文件vim mysql_config,找到libs="$libs -l ",改为libs="$libs -lmysqlclient -lssl -lcrypto "
libs="-L$pkglibdir"
# libs="$libs -l " # 原来的
libs="$libs -lmysqlclient -lssl -lcrypto " # 更改后的
embedded_libs="-L$pkglibdir"
embedded_libs="$embedded_libs -l "
- 再来一遍
pip install MySQL-python
- 终于成功了!可喜可贺!可喜可贺!
- 小心翼翼的试一下,
import MySQLdb,真的成功了
发现第四个问题
- 开始使用的时候,发现自己用的是
python2.x的环境,换成python3.x继续用
- 在
import MySQLdb的时候又出问题了,ModuleNotFoundError: No module named 'MySQLdb'
-
尝试使用pip3 install MySQL-python再安装一次,报错
Collecting MySQL-python
Using cached https://files.pythonhosted.org/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/private/var/folders/07/v8slhf9x5zsbbd8_9cd5ztnh0000gp/T/pip-install-oh_74ez5/MySQL-python/setup.py", line 13, in <module>
from setup_posix import get_config
File "/private/var/folders/07/v8slhf9x5zsbbd8_9cd5ztnh0000gp/T/pip-install-oh_74ez5/MySQL-python/setup_posix.py", line 2, in <module>
from ConfigParser import SafeConfigParser
ModuleNotFoundError: No module named 'ConfigParser'
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/07/v8slhf9x5zsbbd8_9cd5ztnh0000gp/T/pip-install-oh_74ez5/MySQL-python/
解决第四个问题
In Python 3, ConfigParser has been renamed to configparser for PEP 8 compliance. It looks like the package you are installing does not support Python 3.
在Python3中,ConfigParser为了符合PEP8规范,已重命名为configparser。看起来你正在安装的软件包不支持Python3。
参考链接:时光不写博客-_mysql.c:44:10:fatal error:'my_config.h'...