最近在學些Python時搭建相關環境,想在本地Mac上裝上mysqlclient,但著實遇到不少坑,最終還是在github issue中找到了解決方法,這里記錄一下,也讓遇到同樣問題的朋友快速解決。
首先,需要安裝mysql,并依賴openssl,這個就不說了,直接官網下載mysql安裝包無腦安裝就好了
這時,你想試一下安裝mysqlclient,于是通過pip進行安裝嘗試:
pip3 install mysqlclient
這時你會得到一個錯誤:
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/0h/jn4vphd94wsf0sl01jb74p200000gn/T/pip-build-75a5emrb/mysqlclient/
別急,照著步驟做:
通過brew安裝 mysql-connector-c
brew install mysql-connector-c
安裝后你可以找到mysql_config這個源文件,在里面找到如下內容
# Create options
libs="-L$pkglibdir"
libs="$libs -l "
將他替換成
# Create options
libs="-L$pkglibdir"
libs="$libs -lmysqlclient -lssl -lcrypto"
這時嘗試安裝mysqlclient,又出現另一個錯誤
Command "/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/private/var/folders/0h/jn4vphd94wsf0sl01jb74p200000gn/T/pip-build-7dif93uz/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 /var/folders/0h/jn4vphd94wsf0sl01jb74p200000gn/T/pip-ljzyp5xt-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/var/folders/0h/jn4vphd94wsf0sl01jb74p200000gn/T/pip-build-7dif93uz/mysqlclient/
不要急,這時因為沒有openssl環境變量,這樣做
export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"
最后,再次安裝mysqlclient
pip3 install mysqlclient
你會看到
Collecting mysqlclient
Using cached mysqlclient-1.3.12.tar.gz
Installing collected packages: mysqlclient
Running setup.py install for mysqlclient ... done
Successfully installed mysqlclient-1.3.12
done!