Mac安装Mysql-python遇到的坑,被这俩报错反复摩擦:'my_config.h' file not found 和 IndexError: string index out of range

最后Stackoverflow上面的大神解决了问题: Link

brew install mysql
brew unlink mysql
brew install mysql-connector-c
sed -i -e 's/libs="$libs -l "/libs="$libs -lmysqlclient -lssl -lcrypto"/g' /usr/local/bin/mysql_config
pip install MySQL-python
brew unlink mysql-connector-c
brew link --overwrite mysql

尤其这条 sed -i -e 's/libs="$libs -l "/libs="$libs -lmysqlclient -lssl -lcrypto"/g' /usr/local/bin/mysql_config
解决了配置文件的问题,最后安装成功了


以上
作者:Jorah_Bronn
来源:CSDN
原文:https://blog.csdn.net/Eri_Feng/article/details/81224106
版权声明:本文为博主原创文章,转载请附上博文链接!

以上实测,我反正是没有搞定
又修改了下mysql_config实现了:
sed -i -e 's/libs="$libs -lmysqlclient -lssl -lcrypto"/# libs="$libs -l "/g' /usr/local/bin/mysql_config

安装好MySQLdb后,在python环境中导入MySQLdb,可能遇到如下问题:

>>> import MySQLdb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/MySQLdb/__init__.py", line 19, in <module>
    import _mysql
ImportError: dlopen(/Library/Python/2.7/site-packages/_mysql.so, 2): Library not loaded: @rpath/libmysqlclient.21.dylib
  Referenced from: /Library/Python/2.7/site-packages/_mysql.so
  Reason: image not found

为了解决上述问题,你可能需要:

otool -L /Library/Python/2.7/site-packages/_mysql.so

该命令的结果可能是:

/Library/Python/2.7/site-packages/_mysql.so:
    @rpath/libmysqlclient.21.dylib (compatibility version 21.0.0, current version 21.0.0)
    libssl.1.0.0.dylib (compatibility version 1.0.0, current version 1.0.0)
    libcrypto.1.0.0.dylib (compatibility version 1.0.0, current version 1.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.50.4)

对此,你需要将前三个lib的位置重新写一下,具体命令:

sudo install_name_tool -change @rpath/libmysqlclient.21.dylib /usr/local/mysql/lib/libmysqlclient.21.dylib /Library/Python/2.7/site-packages/_mysql.so
sudo install_name_tool -change libssl.1.0.0.dylib /usr/local/mysql/lib/libssl.1.0.0.dylib /Library/Python/2.7/site-packages/_mysql.so
sudo install_name_tool -change libcrypto.1.0.0.dylib /usr/local/mysql/lib/libcrypto.1.0.0.dylib /Library/Python/2.7/site-packages/_mysql.so

最终,可以看到不再报错:

>>> import MySQLdb
>>>
>>>

此外,你可能遇到的问题:

>>> import MySQLdb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/MySQLdb/__init__.py", line 19, in <module>
    import _mysql
ImportError: dlopen(/Library/Python/2.7/site-packages/_mysql.so, 2): Symbol not found: _mysql_affected_rows
  Referenced from: /Library/Python/2.7/site-packages/_mysql.so
  Expected in: flat namespace
 in /Library/Python/2.7/site-packages/_mysql.so

这个问题的原因是,你的Pyhon和Mysql的版本不一致导致的。
检查python的版本

import platform
platform.architecture() 

比如我的mysql版本是64位,mac自带的python版本是2.7.10(32 bit)。我自己的解决方法是重新下载了python2.7.15(64bit),并做了如下配置(vim ~/.bash_profile):

PATH="/usr/local/mysql/bin:${PATH}"
export PATH
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/
export VERSIONER_PYTHON_PREFER_64_BIT=yes
export VERSIONER_PYTHON_PREFER_32_BIT=no
posted @ 2019-07-15 11:39  梦溺残沙里  阅读(1348)  评论(0编辑  收藏  举报