Mac 下安装 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

此时可成功安装

$ pip install MySQL-Python

Collecting MySQL-Python
  Using cached https://pypi.doubanio.com/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip
Building wheels for collected packages: MySQL-Python
  Building wheel for MySQL-Python failed: [Errno 13] Permission denied: '/Users/evil/Library/Caches/pip/wheels/5a'
Failed to build MySQL-Python
Installing collected packages: MySQL-Python
  Running setup.py install for MySQL-Python ... done
Successfully installed MySQL-Python-1.2.5

你可能感兴趣的:(Mac 下安装 MySQL-Python)