Mac上安装mysqlclient报错:OSError: mysql_config not found
pip安装mysqlclient时报错如下
Collecting mysqlclient
Downloading http://mirrors.aliyun.com/pypi/packages/d0/97/7326248ac8d5049968bf4ec708a5d3d4806e412a42e74160d7f266a3e03a/mysqlclient-1.4.6.tar.gz (85 kB)
|████████████████████████████████| 85 kB 2.0 MB/s
ERROR: Command errored out with exit status 1:
command: /usr/local/opt/python@3.8/bin/python3.8 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/vd/rc7lfsc90pv16rmt6m3fv62w0000gn/T/pip-install-l3xb37xq/mysqlclient/setup.py'"'"'; __file__='"'"'/private/var/folders/vd/rc7lfsc90pv16rmt6m3fv62w0000gn/T/pip-install-l3xb37xq/mysqlclient/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /private/var/folders/vd/rc7lfsc90pv16rmt6m3fv62w0000gn/T/pip-install-l3xb37xq/mysqlclient/pip-egg-info
cwd: /private/var/folders/vd/rc7lfsc90pv16rmt6m3fv62w0000gn/T/pip-install-l3xb37xq/mysqlclient/
Complete output (12 lines):
/bin/sh: mysql_config: command not found
/bin/sh: mariadb_config: command not found
/bin/sh: mysql_config: command not found
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/private/var/folders/vd/rc7lfsc90pv16rmt6m3fv62w0000gn/T/pip-install-l3xb37xq/mysqlclient/setup.py", line 16, in <module>
metadata, options = get_config()
File "/private/var/folders/vd/rc7lfsc90pv16rmt6m3fv62w0000gn/T/pip-install-l3xb37xq/mysqlclient/setup_posix.py", line 61, in get_config
libs = mysql_config("libs")
File "/private/var/folders/vd/rc7lfsc90pv16rmt6m3fv62w0000gn/T/pip-install-l3xb37xq/mysqlclient/setup_posix.py", line 29, in mysql_config
raise EnvironmentError("%s not found" % (_mysql_config_path,))
OSError: mysql_config not found
解决方法如下:
安装 mysql-connector-c:
brew install mysql-connector-c
查找mysql_config的位置:
cd /usr/local/bin
find / -name mysql_config
输出:
/usr/local/bin/mysql_config
/usr/local/Cellar/mysql-connector-c/6.1.11/bin/mysql_config
将/usr/local/Cellar/mysql-connector-c/6.1.11/bin/mysql_config配置到环境变量中:
vi ~/.bash_profile
配置:
PATH=/bin:/usr/bin:/usr/local/bin/:usr/local/Cellar/mysql-connector-c/6.1.11/bin/:${PATH}
export PATH
其中usr/local/Cellar/mysql-connector-c/6.1.11/bin/
为新配置的环境变量。
使新配置的~/.bash_profile生效:
source ~/.bash_profile
备注:如果没有效果,需要在pycharm中的终端运行
source ~/.bash_profile
最后,执行安装命令即可:
pip install mysqlclient==1.4.4
最后,mysqlclient安装成功。