对于 mysqldb-python 的安装过程,心中有无数只草泥马奔腾而过。过程是在是艰辛。iMac 装好了,在自己的MBP 装,一直报
Command "python setup.py egg_info" failed with error code 1
的错误,因为一开始
装Python时版本搞错了,导致后面卸载重装还是一样的结果,没办法只能换成pymysql
,下面就来说说安装步骤
通过 pip 指令安装
pip install mysqlclient
pip install MySQL-python
export PATH=$PATH:/usr/local/mysql/bin
Reason: image not found
安装完MySQL-python包后,让我们import MySQLdb,此时出现一个错误,错误最后一行写着 Reason: image not found。
找不到libmysqlclient.18.dylib这个文件,但这个文件在/usr/local/mysql/lib/下是有的, 只是位置不对,于是我们这样解决.
执行命令行
$ sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
结果提示:
ln: /usr/lib/libmysqlclient.18.dylib: Operation not permitted
原因:
The computer security settings prevent the shared library _mysql.so from using a relative reference to the library libmysqlclient.18.dylib. In the future, the shared library _mysql.so may be updated. Until then, you can force it to use an absolute reference via the install_name_toolutility. Assuming that libmysqlclient.18.dylib is in /usr/local/mysql/lib/, then run the command
解决办法:
sudo install_name_tool -change libmysqlclient.18.dylib \
/usr/local/mysql/lib/libmysqlclient.18.dylib \
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/_mysql.so
然后再执行
$ sudo ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/sc/qjyw8_3d3j3cz1xrh894lps40000gn/T/pip-build-5cAMC4/MySQL-python/
解决方法摘录
Python报错: Command “python setup.py egg_info” failed with error code 1 in /private/var/folders/0m/7s1flm9j03d35pkr92jcsv3w0000gq/T/pip-build-fK6SMy/aiohttp/
这是因为你电脑同时用 Python2.7+ 和3.5+ 版本. 默认pip会使用 2.7+ 版本,而有些库依赖3.5+版本.
这时只需要指定使用 3.5+ 对应的pip版本即可.
sudo python3 -m pip install aiohttp
1
注意: 有时候你可能需要先更新一下pip版本
sudo python3 -m pip install --upgrade pip
但是,我是用2.7的,怎么装都不对。后来换成 pymysql ,下面做下摘录,来源地址:http://codepad.org/wPOPGC5D
后来,我换了 pymysql...
(os x 10.10.2 + python 3.4.2 + mysql 5.6.22 (Homebrew 安装))
1. $ sudo pip3 install PyMySQL
参考 PyMySQL 的 github 页
2. 在工程的 __init__.py 添加:
import pymysql
pymysql.install_as_MySQLdb()
3. 在 settings.py:
'ENGINE': 'django.db.backends.mysql', # 保持默认不变
安装成功的验证:
以前,python3 manage.py syncdb 会提示 No module named 'MySQLdb'
安好后,python3 manage.py syncdb 就正常运行……
摘录:一场由mysql官方引发的python血案
出现这个问题的原因是mysql-connector-c中配置项有误。
具体针对mac来说,你需要顺藤摸瓜找到mysql_config的真身,即
/usr/local/Cellar/mysql-connector-c/6.1.11/bin/mysql_config
cd /usr/local/Cellar/mysql-connector-c/6.1.11/bin/mysql_config
# 修改前先备份
cp mysql_config mysql_config.backup
# 使用vi修改配置文件
sudo vi mysql_config
# 114 gg跳转到 114行
将
> # Create options
> libs="-L$pkglibdir"
> libs="$libs -l "
替换为
> # Create options
> libs="-L$pkglibdir"
> libs="$libs -lmysqlclient -lssl -lcrypto"
然后保存即可。
# 然后重新运行mysqlclient安装命令,之后一切顺利,大功告成
pip install mysqlclient
解决Python3下mysqldb不支持
MySQLdb 只适用于python2.x,发现pip装不上。它在py3的替代品是:import pymysql
安装pymysql
import pymysql
pymysql.install_as_MySQLdb()
参考
https://segmentfault.com/q/1010000002539165
https://stackoverflow.com/questions/5178292/pip-install-mysql-python-fails-with-environmenterror-mysql-config-not-found
http://blog.csdn.net/janronehoo/article/details/25207825
http://blog.csdn.net/wirelessqa/article/details/50506056
https://stackoverflow.com/questions/5178292/pip-install-mysql-python-fails-with-environmenterror-mysql-config-not-found
Mac OS X下修复Reason: image not found