python安装MySQLdb模块的踩坑之路(mac)

 

最近python连接mysql需要用到MySQLdb,安装这玩意折腾了我好几个小时,下面就请见证一位python小白的精彩踩坑之旅。

 

下载MySQL-python

这个很简单吧,参考:http://www.runoob.com/python/python-mysql.html

  1. 下载 MySQL-python-1.2.4b4.tar.gz  
  2. 执行命令: 
$ gunzip MySQL-python-1.2.4b4.tar.gz
$ tar -xvf MySQL-python-1.2.4b4.tar 

解压完成,最简单的部分搞定了,踩坑之路开始了

 

build踩坑系列

  • 进入解压路径。执行python setup.py build, 报错403 Forbidden 
~/Software/MySQL-python-1.2.4b4 $ python setup.py build
Downloading http://pypi.python.org/packages/source/d/distribute/distribute-0.6.28.tar.gz
Traceback (most recent call last):
  File "setup.py", line 7, in 
    use_setuptools()
  File "/Users/willowzhang/Software/MySQL-python-1.2.4b4/distribute_setup.py", line 145, in use_setuptools
    return _do_download(version, download_base, to_dir, download_delay)
  File "/Users/willowzhang/Software/MySQL-python-1.2.4b4/distribute_setup.py", line 124, in _do_download
    to_dir, download_delay)
  File "/Users/willowzhang/Software/MySQL-python-1.2.4b4/distribute_setup.py", line 194, in download_setuptools
    src = urlopen(url)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 437, in open
    response = meth(req, response)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 550, in http_response
    'http', request, response, code, msg, hdrs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 475, in error
    return self._call_chain(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 558, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 403: Forbidden

解决方法: 直接下载报错的 distribute-0.6.28.tar.gz 包到当前路径即可。

下载路径: http://pypi.python.org/packages/source/d/distribute/distribute-0.6.28.tar.gz 

问题参考:https://blog.csdn.net/xianglingchuan/article/details/78884368

 

  • 继续 python setup.py build,又报错啦,幸好错误信息不一样的。
~/Software/MySQL-python-1.2.4b4 $ python setup.py build
... ... (省略)
running build_ext
building '_mysql' extension
creating build/temp.macosx-10.13-intel-2.7
cc -fno-strict-aliasing -fno-common -dynamic -arch x86_64 -arch i386 -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch x86_64 -pipe -Dversion_info=(1,2,4,'beta',4) -D__version__=1.2.4b4 -I/usr/local/mysql/include -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _mysql.c -o build/temp.macosx-10.13-intel-2.7/_mysql.o
_mysql.c:44:10: fatal error: 'my_config.h' file not found
#include "my_config.h"
         ^
1 error generated.
error: command 'cc' failed with exit status 1

解决方法:怀疑是mysql的配置路径不对,找不到对应文件。编辑 site.cfg  「#mysql_config = /usr/local/bin/mysql_config」 去掉注释, 改成真正的mysql目录 :「mysql_config = /usr/local/mysql/bin/mysql_config」  (这里是我机器上真正的mysql位置)

问题参考:https://www.jianshu.com/p/ca0281c96fe4 中间部分

 

  • OK, 继续 python setup.py build,又又报错啦,错误信息还是一样的,所以问题还没解决。

解决方法:原因是因为我的MySQL不是单独安装的,是安装的一个MAMP集成开发环境包,在MAMP中PHP和MySQL的头文件都是没有的,所以会报出这个错误。我们有两种解决方式,一种是重新编译MySQL,一种是单独安装mysq-connector-c,我决定安装mysq-connector-c吧,比较简单。

问题参考: https://stackoverflow.com/questions/12218229/my-config-h-file-not-found-when-intall-mysql-python-on-osx-10-8 、https://blog.csdn.net/qw_xingzhe/article/details/52695602 

好了,开始安装mysql-connector-c!于是下一波坑又等着我了。

 

mysql-connector-c踩坑系列

本以为安装这玩意很简单,不就是一句 「brew install mysql-connector-c」 的事情么!万万妹想到啊!

  • 执行 brew install mysql-connector-c ,不负众望,也报错啦
~/Software/MySQL-python-1.2.4b4 $ brew install mysql-connector-c 
... ... (省略)

---------------------------------------------
Log of errors: /private/tmp/cmake20180910-85154-4ttw00/cmake-3.2.2/Bootstrap.cmk/cmake_bootstrap.log
---------------------------------------------
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': no implicit conversion of nil into String (TypeError)
	from /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
	from /usr/local/Library/ENV/4.3/gcc-4.2:4:in `
' READ THIS: https://git.io/brew-troubleshooting /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/open-uri.rb:359:in `open_http': 422 Unprocessable Entity (GitHub::Error) from /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/open-uri.rb:737:in `buffer_open' from /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/open-uri.rb:212:in `block in open_loop' from /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/open-uri.rb:210:in `catch' from /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/open-uri.rb:210:in `open_loop' from /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/open-uri.rb:151:in `open_uri' from /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/open-uri.rb:717:in `open' from /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/open-uri.rb:31:in `open' from /usr/local/Library/Homebrew/utils.rb:354:in `open' from /usr/local/Library/Homebrew/utils.rb:384:in `issues_matching' from /usr/local/Library/Homebrew/utils.rb:412:in `issues_for_formula' from /usr/local/Library/Homebrew/exceptions.rb:145:in `fetch_issues' from /usr/local/Library/Homebrew/exceptions.rb:141:in `issues' from /usr/local/Library/Homebrew/exceptions.rb:184:in `dump' from /usr/local/Library/brew.rb:162:in `rescue in
' from /usr/local/Library/brew.rb:65:in `
'

解决方法:看到说可能需要更新brew,好吧那应该执行: brew update  就ok了吧。

问题参考: https://stackoverflow.com/questions/36386761/error-while-installing-pyqt-v4-8-on-mac

 

只有想不到,没有不可能!新一轮的坑又来啦!ヽ(`Д´)ノ

  • brew update 报错说文件冲突 
~/Software/MySQL-python-1.2.4b4 $ brew update
error: Your local changes to the following files would be overwritten by merge:
... ... (省略)
Please commit your changes or stash them before you merge.
Aborting
Error: Failure while executing: git pull -q origin refs/heads/master:refs/remotes/origin/master

解决方法:怀疑是不是brew版本太老了。不管了,为了搞定这个事情,决定重装brew!

 

重装brew

斗志在昂扬!!感觉胜利就在前方!!! 

重装brew就简单多啦,顺便发现我的xcode也需要更新。

参考: https://github.com/Homebrew/install 、 https://www.jianshu.com/p/bca8fc1ff3f0

  • 执行 xcode-select --install(执行完后再测试发现还是不行,看来还是需要更新brew)
  • 先卸载旧版本brew : /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
  • 安装新版本 : /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  • 尝试: brew install mysql-connector-c 成功!
$ xcode-select --install 
$ sudo /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
$ brew install mysql-connector-c

到此,mysql-connector-c终于被我装上了!

 

最终的踩坑系列

对!你没看看错,装完mysql-connector-c后,回头build又双叒有坑了,今天 我大概 应该买彩票吧。掀桌!

  • python setup.py clean 没问题,很欣慰
  • python setup.py build 没问题,最后一步了!
  • python setup.py install 报错了!我又要激动了!还好是权限问题!_(:з」∠)_
  • sudo python setup.py install 嗯,非常完美,成功辣!撒花庆祝!

兴冲冲地准备用一把MySQLdb,咦?又双叒叕有坑了! щ(`□´щ) 

  • import MySQLdb 报错 image not found
$ python
>>> import MySQLdb
Traceback (most recent call last):
  File "", line 1, in 
  File "MySQLdb/__init__.py", line 19, in 
    import _mysql
ImportError: dlopen(/Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.13-intel.egg/_mysql.so, 2): Library not loaded: @rpath/libmysqlclient.21.dylib
  Referenced from: /Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.13-intel.egg/_mysql.so
  Reason: image not found

解决方法:因为lib libmysqlclient.21.dylib 路径的问题,需要修改路径,以下命令可以查看到路径信息:

$ otool -L /Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.13-intel.egg/_mysql.so
/Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.13-intel.egg/_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)

@rpath是什么鬼,我不管我决定改成绝对路径,修改命令如下:

(找到libmysqlclient.21.dylib是在mysql下,绝对路径是 : /usr/local/mysql/lib/libmysqlclient.21.dylib)

$ sudo install_name_tool -change @rpath/libmysqlclient.21.dylib  /usr/local/mysql/lib/libmysqlclient.21.dylib /Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.13-intel.egg/_mysql.so

问题参考: https://www.jianshu.com/p/e196d25a8e31 

 

  • 改完继续, import MySQLdb 报错 relative rpath
>>> import MySQLdb
Traceback (most recent call last):
  File "", line 1, in 
  File "MySQLdb/__init__.py", line 19, in 
    import _mysql
ImportError: dlopen(/Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.13-intel.egg/_mysql.so, 2): Library not loaded: libssl.1.0.0.dylib
  Referenced from: /Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.13-intel.egg/_mysql.so
  Reason: unsafe use of relative rpath libssl.1.0.0.dylib in /Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.13-intel.egg/_mysql.so with restricted binary

解决方法:此刻的我已经被折腾的没脾气了,提示说因为用到了不安全的相对路径,肯定是另外几个lib路径的问题!我改!我改还不行么!! (*´ノ皿`)

找到了路径是这俩: /usr/local/Cellar/openssl/1.0.2p/lib/libssl.1.0.0.dylib   /usr/local/Cellar/openssl/1.0.2p/lib/libcrypto.1.0.0.dylib

执行命令修改路径:

$ sudo install_name_tool -change libssl.1.0.0.dylib  /usr/local/Cellar/openssl/1.0.2p/lib/libssl.1.0.0.dylib /Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.13-intel.egg/_mysql.so
$ sudo install_name_tool -change libcrypto.1.0.0.dylib  /usr/local/Cellar/openssl/1.0.2p/lib/libcrypto.1.0.0.dylib /Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.13-intel.egg/_mysql.so

问题参考:https://blog.csdn.net/yelyyely/article/details/41114449 (找路径的参考)

 

终于! import MySQLdb 成功!!!今晚我得大吃一顿!!!!!٩(๑>◡<๑)۶

 

你可能感兴趣的:(python)