Mac上Django2安装mysqlclient失败 解决方案

2019/07/18 22:28 下午 posted in  Python Django

今天安装了一下午的mysqlclient一直失败,直到晚上,现在把处理的流程记录一下。

# 安装mysql驱动,你的电脑很可能已经安装过了
brew install mysql-connector-c

# 安装mysqlclient
pip install mysqlclient

然后万恶的报错了,你懂的。

Collecting mysqlclient
  Using cached mysqlclient-1.4.2.tar.gz
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/private/var/folders/rv/a_a/T/pip-build-nyaa8t95/mysqlclient/setup.py", line 17, in <module>
        metadata, options = get_config()
      File "/private/var/folders/rv/a_a/T/pip-build-nyaa8t95/mysqlclient/setup_posix.py", line 54, in get_config
        libraries = [dequote(i[2:]) for i in libs if i.startswith('-l')]
      File "/private/var/folders/rv/a_a/T/pip-build-nyaa8t95/mysqlclient/setup_posix.py", line 54, in <listcomp>
        libraries = [dequote(i[2:]) for i in libs if i.startswith('-l')]
      File "/private/var/folders/rv/a_a/T/pip-build-nyaa8t95/mysqlclient/setup_posix.py", line 12, in dequote
        if s[0] in "\"'" and s[0] == s[-1]:
    IndexError: string index out of range

出现这个问题的原因是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

然后还是报错了

    ld: library not found for -lssl
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    error: command 'gcc' failed with exit status 1
    ----------------------------------------
ERROR: Command "/Users/wangxiaoming/Desktop/HyterMatrix/Python/mysite/venv/bin/python -u -c 'import setuptools, tokenize;__file__='"'"'/private/var/folders/q0/sp2k72516td35kgjr1h7kty00000gn/T/pip-install-kce8c8yd/mysqlclient/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/q0/sp2k72516td35kgjr1h7kty00000gn/T/pip-record-k3eo05p8/install-record.txt --single-version-externally-managed --compile --install-headers /Users/wangxiaoming/Desktop/HyterMatrix/Python/mysite/venv/include/site/python3.6/mysqlclient" failed with error code 1 in /private/var/folders/q0/sp2k72516td35kgjr1h7kty00000gn/T/pip-install-kce8c8yd/mysqlclient/

似乎没有安装gcc ,安装gcc之前要看装xcode 命令行工具

xcode-select --install

# 然后
brew install gcc

#  然后重新运行mysqlclient安装命令
pip install mysqlclient

然后。。还是报错了

 ERROR: Complete output from command /Users/wangxiaoming/Desktop/HyterMatrix/Python/mysite/venv/bin/python -u -c 'import setuptools, tokenize;__file__='"'"'/private/var/folders/q0/sp2k72516td35kgjr1h7kty00000gn/T/pip-install-db871pdy/mysqlclient/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/q0/sp2k72516td35kgjr1h7kty00000gn/T/pip-record-jp4c8fy1/install-record.txt --single-version-externally-managed --compile --install-headers /Users/wangxiaoming/Desktop/HyterMatrix/Python/mysite/venv/include/site/python3.6/mysqlclient:

这里是需要没有openssl的环境变量。如果是用的venv虚拟环境,需要在虚拟环境里添加

export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"

#  然后重新运行mysqlclient安装命令
pip install mysqlclient

这一次终于出现了

Successfully installed mysqlclient-1.4.2.post1

泪奔了。