天天看點

macOS使用django安裝mysqlclient遇到的問題(mysqlclient 1.3.3 or newer is required)

以後部落格更新在: https://oldpan.me

有兩個月沒有碰django了,沒想到一下從11.3更新到了2.0。django架構團隊真的是很用心。

最近需要使用django搭建一個網站,使用的資料庫是mysql。

mac電腦裡面已經安裝好了mysql-5.7-community。

之前使用的python和mysql的連結子產品是Pymysql,使用django-11.3版本的時候沒有需要問題。但是重新更新了django到2.0,下載下傳好之前的配置後(requests),運作程式發現這個問題:

mysqlclient 1.3.3 or newer is required; you have 0.7.11.None           

好吧,連之前的Pymysql都不能用了,于是進行安裝mysqlclient。

使用pip進行安裝:

pip install mysqlclient           

并沒有那麼順利,立馬進行報錯:

sh: mysql_config: command not found
Traceback (most recent call last):
  File "setup.py", line 15, in <module>
  ...           

原來是沒有找到mysql_config指令,那就進行添加吧:

(my-virtual-env-3.6.2) guoyanzongdeMacBook-Pro:Web_Assignment oldpan$ PATH="$PATH":/usr/local/mysql/bin

(my-virtual-env-3.6.2) guoyanzongdeMacBook-Pro:Web_Assignment oldpan$ mysql_config
Usage: /usr/local/mysql/bin/mysql_config [OPTIONS]
Compiler: Clang 8.1.0
Options:
        --cflags         [-I/usr/local/mysql/include ]
        --cxxflags       [-I/usr/local/mysql/include ]
        --include        [-I/usr/local/mysql/include]
        --libs           [-L/usr/local/mysql/lib -lmysqlclient ]
        --libs_r         [-L/usr/local/mysql/lib -lmysqlclient ]
        --plugindir      [/usr/local/mysql/lib/plugin]
        --socket         [/tmp/mysql.sock]
        --port           [0]
        --version        [5.7.19]
        --libmysqld-libs [-L/usr/local/mysql/lib -lmysqld ]
        --variable=VAR   VAR is one of:
                pkgincludedir [/usr/local/mysql/include]
                pkglibdir     [/usr/local/mysql/lib]
                plugindir     [/usr/local/mysql/lib/plugin]
           

好了,mysql_config能用了,接下來進行安裝吧:

(my-virtual-env-3.6.2) guoyanzongdeMacBook-Pro:Web_Assignment oldpan$ pip install mysqlclient-1.3.12.tar.gz 
Processing ./mysqlclient-1.3.12.tar.gz
Building wheels for collected packages: mysqlclient
  Running setup.py bdist_wheel for mysqlclient ... error
  Complete output from command /usr/local/opt/pyenv/versions/3.6.2/envs/my-virtual-env-3.6.2/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/private/var/folders/m7/1w3636y53qd6k5z6f1hzqhpc0000gn/T/pip-giyw6g30-build/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /var/folders/m7/1w3636y53qd6k5z6f1hzqhpc0000gn/T/tmpj4_ingaepip-wheel- --python-tag cp36:
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.macosx-10.12-x86_64-3.6
  copying _mysql_exceptions.py -> build/lib.macosx-10.12-x86_64-3.6
  creating build/lib.macosx-10.12-x86_64-3.6/MySQLdb
  copying MySQLdb/__init__.py -> build/lib.macosx-10.12-x86_64-3.6/MySQLdb
  copying MySQLdb/compat.py -> build/lib.macosx-10.12-x86_64-3.6/MySQLdb
  copying MySQLdb/connections.py -> build/lib.macosx-10.12-x86_64-3.6/MySQLdb
  copying MySQLdb/converters.py -> build/lib.macosx-10.12-x86_64-3.6/MySQLdb
  copying MySQLdb/cursors.py -> build/lib.macosx-10.12-x86_64-3.6/MySQLdb
  copying MySQLdb/release.py -> build/lib.macosx-10.12-x86_64-3.6/MySQLdb
  copying MySQLdb/times.py -> build/lib.macosx-10.12-x86_64-3.6/MySQLdb
  creating build/lib.macosx-10.12-x86_64-3.6/MySQLdb/constants
  copying MySQLdb/constants/__init__.py -> build/lib.macosx-10.12-x86_64-3.6/MySQLdb/constants
  copying MySQLdb/constants/CLIENT.py -> build/lib.macosx-10.12-x86_64-3.6/MySQLdb/constants
  copying MySQLdb/constants/CR.py -> build/lib.macosx-10.12-x86_64-3.6/MySQLdb/constants
  copying MySQLdb/constants/ER.py -> build/lib.macosx-10.12-x86_64-3.6/MySQLdb/constants
  copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.macosx-10.12-x86_64-3.6/MySQLdb/constants
  copying MySQLdb/constants/FLAG.py -> build/lib.macosx-10.12-x86_64-3.6/MySQLdb/constants
  copying MySQLdb/constants/REFRESH.py -> build/lib.macosx-10.12-x86_64-3.6/MySQLdb/constants
  running build_ext
  building '_mysql' extension
  creating build/temp.macosx-10.12-x86_64-3.6
  clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -Dversion_info=(1,3,12,'final',0) -D__version__=1.3.12 -I/usr/local/mysql/include -I/usr/local/opt/pyenv/versions/3.6.2/include/python3.6m -c _mysql.c -o build/temp.macosx-10.12-x86_64-3.6/_mysql.o
  xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
  error: command 'clang' failed with exit status 1
           

又遇到了問題,經查詢是沒有安裝xcode工具:

打開mac的終端:

xcode-select --install           

過了一兩分鐘後,安裝好了,現在再進行安裝mysqlclient:

(my-virtual-env-3.6.2) guoyanzongdeMacBook-Pro:Web_Assignment oldpan$ pip install mysqlclient-1.3.12.tar.gz 
Processing ./mysqlclient-1.3.12.tar.gz
Building wheels for collected packages: mysqlclient
  Running setup.py bdist_wheel for mysqlclient ... done
  Stored in directory: /Users/oldpan/Library/Caches/pip/wheels/d1/94/21/97409904f278ca3331b447b448a3e90757b24aa72ddc8e22f1
Successfully built mysqlclient
Installing collected packages: mysqlclient
Successfully installed mysqlclient-1.3.12
           

終于安裝好了!

參考資料:

1、

http://blog.csdn.net/kedongjun/article/details/51470506

2、

http://blog.csdn.net/u014642465/article/details/73890308

3、

https://www.cnblogs.com/roystime/p/6920489.html

4、

https://www.cnblogs.com/njj10/p/7676123.html