问题
python27环境直接安装MySQL-python出现以下报错:
pip install MySQL-python
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
Collecting MySQL-python
Using cached https://files.pythonhosted.org/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip
Building wheels for collected packages: MySQL-python
Building wheel for MySQL-python (setup.py) ... error
ERROR: Complete output from command /Users/wx/Desktop/pgo_django/.venv/bin/python2.7 -u -c 'import setuptools, tokenize;__file__='"'"'/private/var/folders/dt/ffnv1q7j0jg5dw5g58gfw66h0000gp/T/pip-install-aGQeuy/MySQL-python/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 /private/var/folders/dt/ffnv1q7j0jg5dw5g58gfw66h0000gp/T/pip-wheel-Bx8yQM --python-tag cp27:
ERROR: running bdist_wheel
running build
running build_py
creating build
creating build/lib.macosx-10.14-x86_64-2.7
copying _mysql_exceptions.py -> build/lib.macosx-10.14-x86_64-2.7
creating build/lib.macosx-10.14-x86_64-2.7/MySQLdb
copying MySQLdb/__init__.py -> build/lib.macosx-10.14-x86_64-2.7/MySQLdb
copying MySQLdb/converters.py -> build/lib.macosx-10.14-x86_64-2.7/MySQLdb
copying MySQLdb/connections.py -> build/lib.macosx-10.14-x86_64-2.7/MySQLdb
copying MySQLdb/cursors.py -> build/lib.macosx-10.14-x86_64-2.7/MySQLdb
copying MySQLdb/release.py -> build/lib.macosx-10.14-x86_64-2.7/MySQLdb
copying MySQLdb/times.py -> build/lib.macosx-10.14-x86_64-2.7/MySQLdb
creating build/lib.macosx-10.14-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/__init__.py -> build/lib.macosx-10.14-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/CR.py -> build/lib.macosx-10.14-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.macosx-10.14-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/ER.py -> build/lib.macosx-10.14-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/FLAG.py -> build/lib.macosx-10.14-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/REFRESH.py -> build/lib.macosx-10.14-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/CLIENT.py -> build/lib.macosx-10.14-x86_64-2.7/MySQLdb/constants
running build_ext
building '_mysql' extension
creating build/temp.macosx-10.14-x86_64-2.7
clang -fno-strict-aliasing -fno-common -dynamic -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 -I/usr/local/Cellar/mysql/8.0.12/include/mysql -I/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _mysql.c -o build/temp.macosx-10.14-x86_64-2.7/_mysql.o
_mysql.c:44:10: fatal error: 'my_config.h' file not found
#include "my_config.h"
^~~~~~~~~~~~~
1 error generated.
error: command 'clang' failed with exit status 1
----------------------------------------
ERROR: Failed building wheel for MySQL-python
Running setup.py clean for MySQL-python
Failed to build MySQL-python
Installing collected packages: MySQL-python
Running setup.py install for MySQL-python ... error
ERROR: Complete output from command /Users/wx/Desktop/pgo_django/.venv/bin/python2.7 -u -c 'import setuptools, tokenize;__file__='"'"'/private/var/folders/dt/ffnv1q7j0jg5dw5g58gfw66h0000gp/T/pip-install-aGQeuy/MySQL-python/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/dt/ffnv1q7j0jg5dw5g58gfw66h0000gp/T/pip-record-PuIVBL/install-record.txt --single-version-externally-managed --compile --install-headers /Users/wx/Desktop/pgo_django/.venv/bin/../include/site/python2.7/MySQL-python:
ERROR: running install
running build
running build_py
creating build
creating build/lib.macosx-10.14-x86_64-2.7
copying _mysql_exceptions.py -> build/lib.macosx-10.14-x86_64-2.7
creating build/lib.macosx-10.14-x86_64-2.7/MySQLdb
copying MySQLdb/__init__.py -> build/lib.macosx-10.14-x86_64-2.7/MySQLdb
copying MySQLdb/converters.py -> build/lib.macosx-10.14-x86_64-2.7/MySQLdb
copying MySQLdb/connections.py -> build/lib.macosx-10.14-x86_64-2.7/MySQLdb
copying MySQLdb/cursors.py -> build/lib.macosx-10.14-x86_64-2.7/MySQLdb
copying MySQLdb/release.py -> build/lib.macosx-10.14-x86_64-2.7/MySQLdb
copying MySQLdb/times.py -> build/lib.macosx-10.14-x86_64-2.7/MySQLdb
creating build/lib.macosx-10.14-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/__init__.py -> build/lib.macosx-10.14-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/CR.py -> build/lib.macosx-10.14-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.macosx-10.14-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/ER.py -> build/lib.macosx-10.14-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/FLAG.py -> build/lib.macosx-10.14-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/REFRESH.py -> build/lib.macosx-10.14-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/CLIENT.py -> build/lib.macosx-10.14-x86_64-2.7/MySQLdb/constants
running build_ext
building '_mysql' extension
creating build/temp.macosx-10.14-x86_64-2.7
clang -fno-strict-aliasing -fno-common -dynamic -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 -I/usr/local/Cellar/mysql/8.0.12/include/mysql -I/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _mysql.c -o build/temp.macosx-10.14-x86_64-2.7/_mysql.o
_mysql.c:44:10: fatal error: 'my_config.h' file not found
#include "my_config.h"
^~~~~~~~~~~~~
1 error generated.
error: command 'clang' failed with exit status 1
----------------------------------------
ERROR: Command ".venv/bin/python2.7 -u -c 'import setuptools, tokenize;__file__='"'"'/private/var/folders/dt/ffnv1q7j0jg5dw5g58gfw66h0000gp/T/pip-install-aGQeuy/MySQL-python/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/dt/ffnv1q7j0jg5dw5g58gfw66h0000gp/T/pip-record-PuIVBL/install-record.txt --single-version-externally-managed --compile --install-headers .venv/bin/../include/site/python2.7/MySQL-python" failed with error code 1 in /private/var/folders/dt/ffnv1q7j0jg5dw5g58gfw66h0000gp/T/pip-install-aGQeuy/MySQL-python/
然后按照网上查到的下载MySQL-python-1.2.5包手动安装,发现也是提示'my_config.h' file not found
错误信息:
MySQL-python-1.2.5 python setup.py build
running build
running build_py
copying MySQLdb/release.py -> build/lib.macosx-10.14-x86_64-2.7/MySQLdb
running build_ext
building '_mysql' extension
creating build/temp.macosx-10.14-x86_64-2.7
clang -fno-strict-aliasing -fno-common -dynamic -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 -I/usr/local/Cellar/mysql/8.0.12/include/mysql -I/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _mysql.c -o build/temp.macosx-10.14-x86_64-2.7/_mysql.o
_mysql.c:44:10: fatal error: 'my_config.h' file not found
#include "my_config.h"
^~~~~~~~~~~~~
1 error generated.
error: command 'clang' failed with exit status 1
解决
配置环境
需要gcc
编译环境,mysql
的C语言库。
➜ which gcc
/usr/bin/gcc
➜ brew install mysql #首先下载mysql
➜ brew unlink mysql # 删除mysql命令
➜ brew install mysql-connector-c # 安装 mysql-connector-c
Warning: mysql-connector-c 6.1.11 is already installed, it's just not linked
You can use `brew link mysql-connector-c` to link this version.
➜ brew link mysql-connector-c
Linking /usr/local/Cellar/mysql-connector-c/6.1.11... 48 symlinks created
### 执行这条命令使其在编译是能够找到mysql的C语言库
➜ sed -i -e 's/libs="$libs -l "/libs="$libs -lmysqlclient -lssl -lcrypto"/g' /usr/local/Cellar/mysql/8.0.12/bin/mysql_config
pip安装
pip install MySQL-python
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
Collecting MySQL-python
Installing collected packages: MySQL-python
Successfully installed MySQL-python-1.2.5
恢复mysql环境变量
brew unlink mysql-connector-c
brew link --overwrite mysql