正常来说直接执行pip安装,就是可以的,但是MySQL-python偏偏比较独特
pip install MySQL-python
报错:
_mysql.c:44:10: fatal error: 'my_config.h' file not found^~~~~~~~~~~~~1 error generated.error: command 'cc' failed with exit status 1
解决第一个问题
执行brew install mysql-connector-c
brew install mysql-connector-c
如果这一步直接完成,那就可以继续pip install MySQL-python了,应该会成功
但是我在这一步执行失败了
遇到第二个问题
brew install mysql-connector-c报错
Error: Cannot install mysql-connector-c because conflicting formulae are installed.mysql: because both install MySQL client librariesPlease `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 mysqlDownloading 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.zipComplete output from command python setup.py egg_info:Traceback (most recent call last):File "", line 1, in <module>File "/private/var/folders/zn/t8xxx4m149s9jqp1810ndrz80000gn/T/pip-install-oHMKPE/MySQL-python/setup.py", line 17, in <module>metadata, options &#61; get_config()File "setup_posix.py", line 53, in get_configlibraries &#61; [ dequote(i[2:]) for i in libs if i.startswith(compiler_flag("l")) ]File "setup_posix.py", line 8, in dequoteif s[0] in "\"&#39;" and s[0] &#61;&#61; s[-1]:IndexError: string index out of range
解决第三个问题
修改mysql的配置文件mysql_config&#xff0c;修改前记得cp一下
执行mysql_config&#xff0c;查看一下路径
打开文件vim mysql_config&#xff0c;找到libs&#61;"$libs -l "&#xff0c;改为
libs&#61;"$libs -lmysqlclient -lssl -lcrypto "libs&#61;"-L$pkglibdir"
libs&#61;"$libs -lmysqlclient -lssl -lcrypto "
embedded_libs&#61;"-L$pkglibdir"
embedded_libs&#61;"$embedded_libs -l "
如果继续报错提示&#xff1a;
ld: library not found for -lssl
可以删除掉 -lssl即可&#xff0c;同理如果提示&#xff1a;
ld: library not found for -lcrypto
删除-lcrypto即可
再来一遍pip install MySQL-python
终于成功了&#xff01;可喜可贺&#xff01;可喜可贺&#xff01;大功告成&#xff01;&#xff01;&#xff01;
小心翼翼的试一下&#xff0c;import MySQLdb&#xff0c;真的成功了
发现第四个问题
开始使用的时候&#xff0c;发现自己用的是python2.x的环境&#xff0c;换成python3.x继续用
在import MySQLdb的时候又出问题了&#xff0c;ModuleNotFoundError: No module named ‘MySQLdb’
尝试使用pip3 install MySQL-python再安装一次&#xff0c;报错
Collecting MySQL-pythonUsing cached https://files.pythonhosted.org/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zipComplete output from command python setup.py egg_info:Traceback (most recent call last):File "", 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_configFile "/private/var/folders/07/v8slhf9x5zsbbd8_9cd5ztnh0000gp/T/pip-install-oh_74ez5/MySQL-python/setup_posix.py", line 2, in <module>from ConfigParser import SafeConfigParserModuleNotFoundError: No module named &#39;ConfigParser&#39;----------------------------------------
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/
解决第四个问题
查到了原因&#xff0c;感到一阵阵的无语
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中&#xff0c;ConfigParser为了符合PEP8规范&#xff0c;已重命名为configparser。看起来你正在安装的软件包不支持Python3。
因为不支持python3&#xff0c;建议使用pip install pymysql&#xff0c;安装也没那么多套路
其实也找到了解决方案&#xff08;没有测试&#xff0c;我也不知道对不对&#xff0c;单纯的记录一下&#xff09;
方法一, 修改six模块为
try:import configparser
except:from six.moves import configparser
方法二
cp /usr/local/lib/python3.7/configparser.py /usr/local/lib/python3.7/ConfigParser.py
修改转自&#xff1a;https://www.wxy.email/2019/06/26/MySQL-python/