在mac环境下使用pip install mysql-python会出现如下错误
edz@bogon oss % pip 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
Collecting MySQL-python
Using cached MySQL-python-1.2.5.zip (108 kB)
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/pc/g_0g8z4x2j1bkwklngj82dr80000gn/T/pip-install-Asn9Nq/mysql-python/setup.py'"'"'; __file__='"'"'/private/var/folders/pc/g_0g8z4x2j1bkwklngj82dr80000gn/T/pip-install-Asn9Nq/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/pc/g_0g8z4x2j1bkwklngj82dr80000gn/T/pip-wheel-8fR9hH
...
running build_ext
building '_mysql' extension
creating build/temp.macosx-10.15-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 -arch x86_64 -pipe -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 -I/usr/local/Cellar/mysql/8.0.23/include/mysql -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _mysql.c -o build/temp.macosx-10.15-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
Failed to build MySQL-python
Installing collected packages: MySQL-python
Running setup.py install for MySQL-python ... 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/pc/g_0g8z4x2j1bkwklngj82dr80000gn/T/pip-install-Asn9Nq/mysql-python/setup.py'"'"'; __file__='"'"'/private/var/folders/pc/g_0g8z4x2j1bkwklngj82dr80000gn/T/pip-install-Asn9Nq/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/pc/g_0g8z4x2j1bkwklngj82dr80000gn/T/pip-record-gDcVpv/install-record.txt --single-version-externally-managed --user --prefix= --compile --install-headers /Users/edz/Library/Python/2.7/include/python2.7/MySQL-python
...
running build_ext
building '_mysql' extension
creating build/temp.macosx-10.15-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 -arch x86_64 -pipe -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 -I/usr/local/Cellar/mysql/8.0.23/include/mysql -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _mysql.c -o build/temp.macosx-10.15-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: Command errored out with exit status 1: /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/pc/g_0g8z4x2j1bkwklngj82dr80000gn/T/pip-install-Asn9Nq/mysql-python/setup.py'"'"'; __file__='"'"'/private/var/folders/pc/g_0g8z4x2j1bkwklngj82dr80000gn/T/pip-install-Asn9Nq/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/pc/g_0g8z4x2j1bkwklngj82dr80000gn/T/pip-record-gDcVpv/install-record.txt --single-version-externally-managed --user --prefix= --compile --install-headers /Users/edz/Library/Python/2.7/include/python2.7/MySQL-python Check the logs for full command output.
解决方案:
- 安装mysql-connector-c
brew install mysql-connector-c
- 执行brew info openssl
edz@bogon mysql % brew info openssl
openssl@1.1: stable 1.1.1j (bottled) [keg-only]
Cryptography and SSL/TLS Toolkit
https://openssl.org/
/usr/local/Cellar/openssl@1.1/1.1.1j (8,071 files, 18.5MB)
Poured from bottle on 2021-02-23 at 00:24:31
From: https://mirrors.ustc.edu.cn/homebrew-core.git/Formula/openssl@1.1.rb
License: OpenSSL
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
/usr/local/etc/openssl@1.1/certs
and run
/usr/local/opt/openssl@1.1/bin/c_rehash
openssl@1.1 is keg-only, which means it was not symlinked into /usr/local,
because macOS provides LibreSSL.
If you need to have openssl@1.1 first in your PATH, run:
echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.zshrc
For compilers to find openssl@1.1 you may need to set:
export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"
==> Analytics
install: 744,047 (30 days), 2,285,311 (90 days), 8,399,250 (365 days)
install-on-request: 118a,042 (30 days), 366,615 (90 days), 1,226,324 (365 days)
build-error: 0 (30 days)
- 执行
echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.zshrc
- 将下面语句加入了 bash_profile 并source bash_profile
edz@bogon mysql % export LDFLAGS="-L/usr/local/opt/openssl/lib"
edz@bogon mysql % export CPPFLAGS="-I/usr/local/opt/openssl/include"
- 执行pip install mysql-python安装成功