记录一次macos 安装MySQL-python报错
同事说自己安装MySQL-python报错误,结果我自己试了试也是一样,本次安装总共遇到两个错误:_mysql.c:44:10: fatal error: ‘my_config.h’ file not found 和 ld: library not found for -lssl。
1. my_config.h not found解决方案
现象:
pip2 install MySQL-python
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/, http://pypi.garenanow.com/
Collecting MySQL-python
Downloading https://mirrors.aliyun.com/pypi/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip (108 kB)
|████████████████████████████████| 108 kB 364 kB/s
Building wheels for collected packages: MySQL-python
Building wheel for MySQL-python (setup.py) ... error
ERROR: Command errored out with exit status 1:
command: /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/7z/kp0xr4ws26n8qmfvjxdmdb4w0000gy/T/pip-install-x0BvsP/mysql-python/setup.py'"'"'; __file__='"'"'/private/var/folders/7z/kp0xr4ws26n8qmfvjxdmdb4w0000gy/T/pip-install-x0BvsP/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/7z/kp0xr4ws26n8qmfvjxdmdb4w0000gy/T/pip-wheel-P159gW
cwd: /private/var/folders/7z/kp0xr4ws26n8qmfvjxdmdb4w0000gy/T/pip-install-x0BvsP/mysql-python/
Complete output (30 lines):
running bdist_wheel
running build
running build_py
creating build
creating build/lib.macosx-11.5-x86_64-2.7
copying _mysql_exceptions.py -> build/lib.macosx-11.5-x86_64-2.7
creating build/lib.macosx-11.5-x86_64-2.7/MySQLdb
copying MySQLdb/__init__.py -> build/lib.macosx-11.5-x86_64-2.7/MySQLdb
copying MySQLdb/converters.py -> build/lib.macosx-11.5-x86_64-2.7/MySQLdb
copying MySQLdb/connections.py -> build/lib.macosx-11.5-x86_64-2.7/MySQLdb
copying MySQLdb/cursors.py -> build/lib.macosx-11.5-x86_64-2.7/MySQLdb
copying MySQLdb/release.py -> build/lib.macosx-11.5-x86_64-2.7/MySQLdb
copying MySQLdb/times.py -> build/lib.macosx-11.5-x86_64-2.7/MySQLdb
creating build/lib.macosx-11.5-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/__init__.py -> build/lib.macosx-11.5-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/CR.py -> build/lib.macosx-11.5-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.macosx-11.5-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/ER.py -> build/lib.macosx-11.5-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/FLAG.py -> build/lib.macosx-11.5-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/REFRESH.py -> build/lib.macosx-11.5-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/CLIENT.py -> build/lib.macosx-11.5-x86_64-2.7/MySQLdb/constants
running build_ext
building '_mysql' extension
creating build/temp.macosx-11.5-x86_64-2.7
cc -fno-strict-aliasing -fno-common -dynamic -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -iwithsysroot /usr/local/libressl/include -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -pipe -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 -I/usr/local/Cellar/mysql/8.0.26/include/mysql -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _mysql.c -o build/temp.macosx-11.5-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 'cc' failed with exit status 1
----------------------------------------
ERROR: Failed building wheel for MySQL-python
Running setup.py clean for MySQL-python
错误原因:我本地安装的mysql 8.0, /usr/local/include/mysql/ 这个路径下面没有 my_config.h 文件
解决方法:
方法1:拷贝一个别人的my_config.h 文件, 放到您的:/usr/local/include/mysql/ 路径下面,重新执行安装命令
方法2:重新安装mysql, 指定安装5.7版本
brew remove mysql
brew install mysql@5.7
brew link --force mysql@5.7
2. library not found for -lssl 解决方案
现象:
ERROR: Command errored out with exit status 1:
command: /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/7z/kp0xr4ws26n8qmfvjxdmdb4w0000gy/T/pip-install-d3Rm9h/mysql-python/setup.py'"'"'; __file__='"'"'/private/var/folders/7z/kp0xr4ws26n8qmfvjxdmdb4w0000gy/T/pip-install-d3Rm9h/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/7z/kp0xr4ws26n8qmfvjxdmdb4w0000gy/T/pip-wheel-ybQPIo
cwd: /private/var/folders/7z/kp0xr4ws26n8qmfvjxdmdb4w0000gy/T/pip-install-d3Rm9h/mysql-python/
Complete output (79 lines):
running bdist_wheel
running build
running build_py
creating build
creating build/lib.macosx-11.5-x86_64-2.7
copying _mysql_exceptions.py -> build/lib.macosx-11.5-x86_64-2.7
creating build/lib.macosx-11.5-x86_64-2.7/MySQLdb
copying MySQLdb/__init__.py -> build/lib.macosx-11.5-x86_64-2.7/MySQLdb
copying MySQLdb/converters.py -> build/lib.macosx-11.5-x86_64-2.7/MySQLdb
copying MySQLdb/connections.py -> build/lib.macosx-11.5-x86_64-2.7/MySQLdb
copying MySQLdb/cursors.py -> build/lib.macosx-11.5-x86_64-2.7/MySQLdb
copying MySQLdb/release.py -> build/lib.macosx-11.5-x86_64-2.7/MySQLdb
copying MySQLdb/times.py -> build/lib.macosx-11.5-x86_64-2.7/MySQLdb
creating build/lib.macosx-11.5-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/__init__.py -> build/lib.macosx-11.5-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/CR.py -> build/lib.macosx-11.5-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.macosx-11.5-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/ER.py -> build/lib.macosx-11.5-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/FLAG.py -> build/lib.macosx-11.5-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/REFRESH.py -> build/lib.macosx-11.5-x86_64-2.7/MySQLdb/constants
copying MySQLdb/constants/CLIENT.py -> build/lib.macosx-11.5-x86_64-2.7/MySQLdb/constants
running build_ext
building '_mysql' extension
creating build/temp.macosx-11.5-x86_64-2.7
cc -fno-strict-aliasing -fno-common -dynamic -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -iwithsysroot /usr/local/libressl/include -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -pipe -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 -I/usr/local/Cellar/mysql@5.7/5.7.35/include/mysql -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _mysql.c -o build/temp.macosx-11.5-x86_64-2.7/_mysql.o
_mysql.c:287:14: warning: implicit conversion loses integer precision: 'Py_ssize_t' (aka 'long') to 'int' [-Wshorten-64-to-32]
cmd_argc = PySequence_Size(cmd_args);
~ ^~~~~~~~~~~~~~~~~~~~~~~~~
_mysql.c:317:12: warning: implicit conversion loses integer precision: 'Py_ssize_t' (aka 'long') to 'int' [-Wshorten-64-to-32]
groupc = PySequence_Size(groups);
~ ^~~~~~~~~~~~~~~~~~~~~~~
_mysql.c:470:14: warning: implicit conversion loses integer precision: 'Py_ssize_t' (aka 'long') to 'int' [-Wshorten-64-to-32]
int j, n2=PySequence_Size(fun);
~~ ^~~~~~~~~~~~~~~~~~~~
_mysql.c:1127:9: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
len = mysql_real_escape_string(&(self->connection), out, in, size);
~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
_mysql.c:1129:9: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
len = mysql_escape_string(out, in, size);
~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
_mysql.c:1168:9: warning: implicit conversion loses integer precision: 'Py_ssize_t' (aka 'long') to 'int' [-Wshorten-64-to-32]
size = PyString_GET_SIZE(s);
~ ^~~~~~~~~~~~~~~~~~~~
/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/stringobject.h:92:32: note: expanded from macro 'PyString_GET_SIZE'
#define PyString_GET_SIZE(op) Py_SIZE(op)
^~~~~~~~~~~
/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/object.h:116:56: note: expanded from macro 'Py_SIZE'
#define Py_SIZE(ob) (((PyVarObject*)(ob))->ob_size)
~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
_mysql.c:1178:9: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
len = mysql_real_escape_string(&(self->connection), out+1, in, size);
~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
_mysql.c:1180:9: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
len = mysql_escape_string(out+1, in, size);
~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
_mysql.c:1274:11: warning: implicit conversion loses integer precision: 'Py_ssize_t' (aka 'long') to 'int' [-Wshorten-64-to-32]
if ((n = PyObject_Length(o)) == -1) goto error;
~ ^~~~~~~~~~~~~~~~~~
/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/abstract.h:434:25: note: expanded from macro 'PyObject_Length'
#define PyObject_Length PyObject_Size
^
_mysql.c:1466:10: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
len = strlen(buf);
~ ^~~~~~~~~~~
_mysql.c:1468:10: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
len = strlen(buf);
~ ^~~~~~~~~~~
_mysql.c:1504:11: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
len = strlen(buf);
~ ^~~~~~~~~~~
_mysql.c:1506:11: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
len = strlen(buf);
~ ^~~~~~~~~~~
13 warnings generated.
cc -bundle -undefined dynamic_lookup -Wl,-F. -L/System/Volumes/Data/SWE/macOS/BuildRoots/d7e177bcf5/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.5.Internal.sdk/usr/local/libressl/lib build/temp.macosx-11.5-x86_64-2.7/_mysql.o -L/usr/local/Cellar/mysql@5.7/5.7.35/lib -lmysqlclient -lssl -lcrypto -o build/lib.macosx-11.5-x86_64-2.7/_mysql.so
ld: warning: directory not found for option '-L/System/Volumes/Data/SWE/macOS/BuildRoots/d7e177bcf5/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.5.Internal.sdk/usr/local/libressl/lib'
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command 'cc' 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
错误原因:cc 编译器使用了默认的ssl库路径,找不到ssl 库的路径
ls /System/Volumes/Data/SWE/macOS/BuildRoots/d7e177bcf5/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.5.Internal.sdk/usr/local/libressl/lib
ls: /System/Volumes/Data/SWE/macOS/BuildRoots/d7e177bcf5/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.5.Internal.sdk/usr/local/libressl/lib: No such file or directory
解决方法:
安装openssl 库, 如果安装了会跳过:
brew update
brew install openssl
echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile
安装完openssl 之后, 在您的 ~/.bash_profile 文件中添加下面的环境:
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib
执行:
source ~/.bash_profile
# 重新安装
pip2 install MySQL-python
亲测安装成功 yyds!