1. 程式人生 > 實用技巧 >Mac上安裝mysqlclient報錯:OSError: mysql_config not found

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/[email protected]/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安裝成功。