安装 mysql-python

$ sw_vers
ProductName:  Mac OS X
ProductVersion: 10.12.6
BuildVersion: 16G29

$ python -V
Python 2.7.14

某个项目开发要用到 MySQL-Python 库,然而 pip install 安装失败,goole 后找到解决方法,总结下。

出错:

Collecting MySQL-Python
  Using cached https://pypi.doubanio.com/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip
    Complete output from command python setup.py egg_info:
    sh: mysql_config: command not found
    Traceback (most recent call last):
      File "", line 1, in 
      File "/private/var/folders/bb/skpfxrzj1jv863wm99wcb_z00000gp/T/pip-build-IiQWYZ/MySQL-Python/setup.py", line 17, in 
        metadata, options = get_config()
      File "setup_posix.py", line 43, in get_config
        libs = mysql_config("libs_r")
      File "setup_posix.py", line 25, in mysql_config
        raise EnvironmentError("%s not found" % (mysql_config.path,))
    EnvironmentError: mysql_config not found

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/bb/skpfxrzj1jv863wm99wcb_z00000gp/T/pip-build-IiQWYZ/MySQL-Python/

mysql_config 在 Mac 下是由 mysql-connector-c 提供的

$ brew install mysql
$ brew install mysql-connector-c

Collecting MySQL-Python
  Using cached https://pypi.doubanio.com/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip
    Complete output from command python setup.py egg_info:
    sh: mysql_config: command not found
    Traceback (most recent call last):
      File "", line 1, in 
      File "/private/var/folders/bb/skpfxrzj1jv863wm99wcb_z00000gp/T/pip-build-IiQWYZ/MySQL-Python/setup.py", line 17, in 
        metadata, options = get_config()
      File "setup_posix.py", line 43, in get_config
        libs = mysql_config("libs_r")
      File "setup_posix.py", line 25, in mysql_config
        raise EnvironmentError("%s not found" % (mysql_config.path,))
    EnvironmentError: mysql_config not found

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/bb/skpfxrzj1jv863wm99wcb_z00000gp/T/pip-build-IiQWYZ/MySQL-Python/

但是安装完 mysql 相关依赖后依旧报错,后来搜到要修改 mysql_config 配置解决

$ which mysql_config
/usr/local/bin/mysql_config

$ vim /usr/local/bin/mysql_config

libs="$libs -l "
修改为
libs="$libs -lmysqlclient -lssl -lcrypto"

ref: https://github.com/PyMySQL/mysqlclient-python/issues/169

此时可成功安装

作者:无限小BUG
链接:https://www.jianshu.com/p/c9ded7a8bf81
來源:
著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

你可能感兴趣的:(安装 mysql-python)